views:

257

answers:

2

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"/>
A: 

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.

marc_s
+1  A: 

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

Tim Jansen
Thanks. I will validate it with XNavigator.
Dmitry