There are a ton of examples of this dilemma online but I still can't quite understand the best way to go about this. I'm trying to create a schema that will:
- Check for specific element order/sequencing
- Each element is mandatory, appears only 1x, and is always listed in the same order.
- Make sure element restrictions apply only to said element (including attribute names and values)
- There's only one element with
type="System.Drawing.Size"but multiple elements withtype="System.Boolean"as well astype="System.String". The XML below is simplified for the sake of illustration. - There are a few elements with
type="System.String"that have their own respective<val>restriction enumeration values.
- There's only one element with
NOTE: The XML structure I'm working with uses 'True' rather than 'true' (even though a System.Boolean will accept mixed-case variations of "TruE") so I have to restrict using base="xs:string" and an enumeration rather than relying on xs:restriction base="xs:boolean".
I'm getting this error message when validating the xml below against the schema I have so far:
cos-element-consistent: Error for type '#AnonType_Application.UserOptionsconfig'. Multiple elements with name 'option', with different types, appear in the model group.
XML:
<?xml version="1.0" encoding="utf-8"?>
<config>
<Application.UserOptions>
<option name="StartMaximized" type="System.Boolean">
<val>False</val>
</option>
<option name="WindowSize" type="System.Drawing.Size">
<val>1366, 768</val>
</option>
</Application.UserOptions>
</config>
XSD:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="config">
<xs:complexType>
<xs:sequence>
<xs:element name="Application.UserOptions">
<xs:complexType>
<xs:sequence>
<xs:element name="option">
<xs:complexType>
<xs:sequence>
<xs:element name="val">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="True"/>
<xs:enumeration value="False"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required" fixed="StartMaximized"/>
<xs:attribute name="type" type="xs:string" use="required" fixed="System.Boolean"/>
</xs:complexType>
</xs:element>
<xs:element name="option">
<xs:complexType>
<xs:sequence>
<xs:element name="val">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="\d+,\s\d+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required" fixed="WindowSize"/>
<xs:attribute name="type" type="xs:string" use="required" fixed="System.Drawing.Size"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
- How can I fix this schema so that it still checks for element ordering and enforces the restrictions I've defined?
- Would validating against a DTD achieve what I'm trying to do, or is using XSD a better way to go?
I'm prepared for receiving a stiff tone from the community as the topic has been seemingly covered, but after looking at responses to questions similar to mine, I still cannot determine how to incorporate the restrictions I've laid out.
Sample XSD 1.1 using
xs:alternativebased ontypeattribute ofoption: