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:
- XML elements can appear in any order.
- "optionalStringX" and "optionalDependeeX" may or may not be present in a given file.
- If "optionalDependeeX" IS present, then "dependentX_1" and "dependentX_2" are REQUIRED to be present. Otherwise, if "optionalDependeeX" IS NOT present, neither "dependentX_1" nor "dependentX_2" will be present.
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.
- Can these restrictions be represented in an XSD schema? We're trying to stick with XSD if possible since it is my understanding that tools and support for it are alot more widespread than other validation solutions.
- If not, what other XML validator has good support in Python, Perl, and either C or C++ and can handle more complicated document structures than XSD? Would RelaxNG or Schematron be a good fit? Pros and cons? I've never used anything besides XSD.