XSD - Validate attribute value based on attribute value in parent element



Is it possible to define an XSD structure such that and attribute on an element can have a certain value only if an attribute on a parent (direct/indirect) element has a certain value?


Example:



<root>
<child1 myAttr="true">
<child2>
<child3 otherAttr="false" /> <!-- 'otherAttr' can only be 'false' if 'myAttr' is 'true' -->
</child2>
</child1>
</root>


Pseudo Solution:


To add something like <rule condition="@otherAttr == true && //child1/@myAttr != false" /> to the definition of the 'child3' complex type...


Example:



<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://ift.tt/tphNwY"
xmlns="http://ift.tt/141Y6VZ"
targetNamespace="http://ift.tt/141Y6VZ">

<xs:element name="root">
<xs:sequence>
<xs:element name="child1" type="child1Type" />
</xs:sequence>
</xs:element>

<xs:complexType name="child1Type">
<xs:sequence>
<xs:element name="child2" type="child2Type" />
</xs:sequence>
<xs:attribute name="myAttr" type="xs:boolean" use="optional" default="false" />
</xs:complexType>

<xs:complexType name="child2Type">
<xs:sequence>
<xs:element name="child3" type="child3Type" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="child3Type">
<xs:attribute name="otherAttr" type="xs:boolean" use="optional" default="true" />
<rule condition="@otherAttr == true && //child1/@myAttr != false" />
</xs:complexType>

No comments:

Post a Comment