How do you add an attribute to an xml schema element?
I have a project where a xsd document is loaded, and based on that a editor is created for each element. And then xml that implements that schema is loaded into the editor. This could be a simple textbox for a string, a datepicker for a date or a more complex data like a chapter with associated metadata.
The editor is generic so the editor has be generated based on the schema. For simple types ( like string and date) we can use thetype
attribute. But for more complex custom types we need to add a attribute to the xsd document that defines what editor subelement to use.
So I added a custom attribute called UIType
, but this is not allowed by the w3 schema schema. Is there any way I can extend the w3 schema schema, prefably without making a copy of it and adding what I need, but actually extend it? Or is there another smarte开发者_JAVA百科r way of doing this?
Thanks.
Just to make it clear; I do not want to add the attribute to the actual xml data, but to the schema.
Here is an example of the schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
<xs:element name="PublishDate" type="xs:date" />
<xs:element name="Chapters">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Chapter" UIType="ChapterPicker" >
<xs:complexType>
<xs:sequence>
<xs:element name="ID" type="xs:int" />
<xs:element name="FileID" type="xs:int" />
<xs:element name="Title" type="xs:string" />
<xs:element name="Artist" type="xs:string" />
<xs:element name="Year" type="xs:int" />
<xs:element name="Timestamp" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
You already seem to be creating your extended types. You aren't limited to using "xs:int" or "xs:string" as the type. You can define your own types by extracting the complexType elements and naming them. You can then use their name as a type for other elements.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
<xs:element name="PublishDate" type="xs:date" />
<xs:element name="Chapters" type="Chapters_t" />
<xs:complexType name="Chapters_t">
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Chapter" UIType="ChapterPicker" >
<xs:complexType>
<xs:sequence>
<xs:element name="ID" type="xs:int" />
<xs:element name="FileID" type="xs:int" />
<xs:element name="Title" type="xs:string" />
<xs:element name="Artist" type="xs:string" />
<xs:element name="Year" type="xs:int" />
<xs:element name="Timestamp" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
精彩评论