Reputation: 1785
Suppose in XSD we have an element 'answer' defined:
<xs:element name="answer" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:attribute name="name" use="required">
<xs:simpleType>
<xs:restriction base="answer"/>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
in the same document we have an element 'language' defined as:
<xs:element name="language" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:attribute name="name" use="required">
<xs:simpleType>
<xs:restriction base="answer"/>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
Both of these have an entry <xs:restriction base="answer"/>
where the "answer" is an enumeration of predefined values.
So, I need to validate that if exists the "answer" node with name = 'some_answer' there is also exists the "answer" node with name = 'some_answer'
Example:
<answer name="some_answer"/>
<language name="some_answer"/>
Upvotes: 3
Views: 2743
Reputation: 3368
I haven't tried it, but that should be possible using the key and keyref elements in the XML schema. You need to define key/keyref relationships in both directions though.
The relationship from language -> answer is defined like this:
<xs:key name="answerKey">
<xs:selector xpath="/answer"/>
<xs:field xpath="@name"/>
</xs:key>
<xs:keyref name="languageRef" refer="answerKey">
<xs:selector xpath="/language"/>
<xs:field xpath="@name"/>
</xs:keyref>
And then you define it in the other direction as well:
<xs:key name="languageKey">
<xs:selector xpath="/language"/>
<xs:field xpath="@name"/>
</xs:key>
<xs:keyref name="answerRef" refer="languageKey">
<xs:selector xpath="/answer"/>
<xs:field xpath="@name"/>
</xs:keyref>
See http://www.w3.org/TR/xmlschema-0/#specifyingUniqueness and http://www.w3.org/TR/2004/REC-xmlschema-1-20041028/structures.html#element-keyref
Upvotes: 2
Reputation: 754868
You cannot do this kind of validation in XML schema - you cannot reference other node's values, or require one node to be present when a sibling is there (or is missing).
These kind of validations might be handled by other validation checkers, such as Schematron - but the regular XML schema cannot do this.
Upvotes: 0