Reputation: 53
I would like to be able to create xml like this:
<Restaurant>
<Property Name="Bar" Value="true" Type="boolean"/>
<Property Name="Grill" Value="true" Type="boolean"/>
<Property Name="Capacity" Value="120" Type="integer"/>
<Property Name="Size" Value="200.5" Type="decimal" Unit="square meter"/>
</Restaurant>
so I created xsd like this:
<xs:complexType name="Restaurant">
<xs:element name="Property" type="Property" minOccurs="0" maxOccurs="unbounded"/>
</xs:element>
<xs:complexType name="Property">
<xs:attribute name="Name" type="xs:string" use="required"/>
<xs:attribute name="Value" type="xs:anySimpleType" use="required"/>
<xs:attribute name="Type" type="xs:string" use="required"/>
<xs:attribute name="Unit" type="xs:string" use="optional"/>
</xs:complexType>
How to define that attribute "Type" can be only one from schema built-in data types? I don't want to create my own enumeration with all possible types. By this I want to achieve that no one will be able to write:
<Property Name="Capacity" Value="120" Type="myOwnIntType"/>
Upvotes: 0
Views: 87
Reputation: 163262
The kind of XML you want to design is a design that XSD almost goes out of its way to discourage. In fact, the main reason I see people using structures like
<Property Name="Bar" Value="true" Type="boolean"/>
rather than
<Bar>true</Bar>
is that they want the description of the structure to be part of the data rather than to be in a separate schema: having both seems overkill.
So, you can't do it with XSD 1.0. However, XSD 1.1 is far more tolerant of people who want to design the XML their way rather than XSD's way, and introduces the construct of "conditional type assignment" for this kind of use case: where the type of an element can depend on the value of one of its attributes. There are currently two implementations of XSD 1.1, Saxon and Xerces.
Upvotes: 1
Reputation: 21194
Afaik there is no such enumeration, within the "Meta-XSD" (the XSD file which describes XSD) they use "xs:QName" as type. Have you tried it?
Upvotes: 0