I have the following XML document structure that is already in production and cannot be changed:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<root xmlns="http://www.domain_name.com/MySchema">
<optionalString1>a</optionalString1>
<optionalString2>b</optionalString2>
<optionalDependee1>c</optionalDependee1>
<dependent1_1>d</dependent1_1>
<dependent1_2>e</dependent1_2>
<optionalString3>f</optionalString3>
<optionalDependee2>g</optionalDependee2>
<dependent2_1>h</dependent2_1>
<dependent2_2>i</dependent2_2>
<!-- more and more elements -->
</root>
Here are the restrictions I'm trying to express in XSD:
I've tried several varations using 'complexType' and 'group' but they aren't valid XSD (at least according to VS2010). My 'group' implementation is like this:
<xs:schema>
<xs:group name="BaseGroup">
<xs:all>
<xs:element name="optionalString1" type="xs:string" minOccurs="0" />
<xs:element name="optionalString2" type="xs:string" minOccurs="0" />
<xs:element name="optionalString3" type="xs:string" minOccurs="0" />
</xs:all>
</xs:group>
<xs:group name="DependentGroup1">
<xs:all>
<xs:element name="optionalDependee1" type="xs:string" minOccurs="1" />
<xs:element name="dependent1_1" type="xs:string" minOccurs="1" />
<xs:element name="dependent1_2" type="xs:string" minOccurs="1" />
</xs:all>
</xs:group>
<xs:group name="DependentGroup2">
<xs:all>
<xs:element name="optionalDependee2" type="xs:string" minOccurs="1" />
<xs:element name="dependent2_1" type="xs:string" minOccurs="1" />
<xs:element name="dependent2_2" type="xs:string" minOccurs="1" />
</xs:all>
</xs:group>
<xs:group name="Combo1">
<xs:all>
<!-- The 'http://www.w3.org/2001/XMLSchema:group'
element is not supported in this context. -->
<xs:group ref="BaseGroup"/>
<xs:group ref="DependentGroup1"/>
</xs:all>
</xs:group>
<xs:group name="Combo2">
<xs:all>
<!-- The 'http://www.w3.org/2001/XMLSchema:group'
element is not supported in this context. -->
<xs:group ref="BaseGroup"/>
<xs:group ref="DependentGroup1"/>
<xs:group ref="DependentGroup2"/>
</xs:all>
</xs:group>
<xs:complexType name="RootType">
<xs:choice minOccurs="1" maxOccurs="1">
<xs:group ref="BaseGroup" />
<xs:group ref="Combo1" />
<xs:group ref="Combo2" />
</xs:choice>
</xs:complexType>
<xs:element name="root" type="RootType" />
</xs:schema>
I'm new to schemas for validating XML and have always assumed until now that XSD was flexible enough to represent the structure of all valid XML. If I could rewrite the structure of the XML files, I'd make dependentX_1 and dependentX_2 either attributes or subelements of optionalDependeeX since those would express a dependent relationship.
As you have correctly done the ordering can be relaxed using xsd:all but the other things you are trying to do is not supported. The next version of XML Schema, which is 1.1, can support some of the other variants you have though. But 1.1 is not final and there are no many libraries that understand this. I'd suggest you look at schematron and see if that can help.