Reputation: 13
Searched and experimented, researched and racked my brain about this (dreamt about it last night).
Trying to build an XSD schema to validate the following example XML based on constraints of the tag depending on the value of the tag.
<data>
<dataSet>
<title>mediaType</title>
<value>FullLength</value>
</dataSet>
<dataSet>
<title>available</title>
<value>true</value>
</dataSet>
<dataSet>
<title>country</title>
<value>Canada</value>
</dataSet>
</data>
the schema regarding constraints on the individual dataSet 's
<xs:complexType name="typeAvailable">
<xs:sequence>
<xs:element name="title">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="available" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="value">
<xs:simpleType>
<xs:restriction base="xs:boolean" />
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="typeMediaType">
<xs:sequence>
<xs:element name="title">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="mediaType" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="value">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="FullLength|Clip" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="typeCountry">
<xs:sequence>
<xs:element name="title">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="typeCountry" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="value">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="Canada|US" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
The best I could come up with doesn't validate when the dataSet 's are out of order (which they will be)
<xs:complexType name="typeData">
<xs:all>
<xs:element name="dataSet" type="typeMediaType" />
<xs:element name="dataSet" type="typeAvailable" />
<xs:element name="dataSet" type="typeCountry" />
</xs:all>
</xs:complexType>
Of course, I'm stuck with the data that I get, but nothing says I can't transform it with XSLT -- into what, I don't know. I was hoping for an elegant XSD solution, alas, I fear this is impossible.
Anyone can prove me wrong? This is my first schema project in quite a long time.
UPDATE
You know what, I think I'm going to validate the structure of it, read the <data> portion w/ LINQ and transform it via XSLT to
<data>
<mediaType>FullLength</mediaType>
<available>true</available>
<country>Canada</country>
</data>
It probably behooves me to do this anyway, as working w/ the data after validation was going to be janky with it in <title><value> pairs any how.
Upvotes: 1
Views: 391
Reputation: 7731
Same-named elements with different types is disallowed by the Element Declarations Consistent constraint.
Upvotes: 0
Reputation: 43709
"Conditional" constraints are not possible with XML Schema. There are few tricks with keys, but they are very limited. Consider using Schematron, it has much richer validation capablities.
Generally, XML Schema is more about structure and typing, its constraining capabilities are very limited.
Upvotes: 1