How to check xml node value in XSD against its neighbor xml node value

Assume that an answer element is defined in XSD:

<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 the 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 them have an entry <xs:restriction base="answer"/> where "answer" is an enumeration of predefined values.

So, I need to check that if there is a β€œresponse” node with the name = 'some_answer' there is also a 'response' node with the name = 'some_answer'

Example:

 <answer name="some_answer"/> <language name="some_answer"/> 
+3
source share
2 answers

I have not tried, but this should be possible using key and keyref elements in an XML schema. You need to define key / keyref relationships in both directions.

Communication with the language β†’ the answer is defined as follows:

 <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 another direction:

 <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

+2
source

You cannot perform such a check in an XML schema β€” you cannot reference other node values ​​or require that one node be present when there is (or is missing) a sibling.

These validations can be handled by other validation validations, such as Schematron , but the regular XML schema cannot do this.

0
source

Source: https://habr.com/ru/post/1305147/


All Articles