I am trying to develop an XSD for some XML that looks roughly like this:
<root>
<thing id="1"/>
<thing id="2"/>
<thing id="3">
<subelement>
...
</subelement>
</thing>
</root>
Where a thing of each id is required once, and the thing with the id of “3” requires specific subelements.
I was able to create the requirement of one thing of each ID by creating a abstract complex type and extending off of that (though it’s still untested),
<xs:element name="thing" type="tns:abstractType" minOccurs="3" maxOccurs="3">
<xs:unique name="EachIdRequired">
<xs:selector xpath="thing" />
<xs:field xpath="@id"/>
</xs:unique>
</xs:element>
<xs:complexType name="abstractType" abstract="true/>
<xs:complexType name="idOne">
<xs:complexContent>
<xs:extension base="tns:abstractType">
<xs:attribute name="id" fixed="1" type="xs:integer" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="idTwo">
<xs:complexContent>
<xs:extension base="tns:abstractType">
<xs:attribute name="id" fixed="2" type="xs:integer" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="idThree">
<xs:complexContent>
<xs:extension base="tns:abstractType">
<xs:attribute name="id" fixed="3" type="xs:integer" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
However, I am unsure how to also have the requirement of the complexType of id 3 to also have the required subelements. The only way I have seen an element defined with both an attribute and subelements is the use of an extension, however idThree is already extending the abstract type.
Is what I am going for possible in some other way? I am doubtful but I want to be sure. XSD seems somewhat limiting.
Not possible in XML Schema. The best Schema langage for such a structure is Relax NG. See http://relaxng.org/