XML Schema force two attributes to be different
In my XML I have defined categories
<category id="3" parent-category-id="2"> <name>Child category</name> </category>
As you can see I have a category element, which can have a parent category defined. Both attributes are defined in my XML Schema file:
<xs:attribute name="id" use="required" type="xs:string" /> <xs:attribute name="parent-category-id" use="optional" type="xs:string" />
I also have keys and keyrefs set in my XSD file. Now I wonder if this is possible to somehow restrict parent-category-id, so that it cannot point to self id. For example I can write:
<category id="3" parent-category-id="3">
and such XML file will still validate, althought it shouldn't.
My keys and keyrefs are:
<xs:key name="ID"> <xs:selector xpath="categories/category" /> <xs:field xpath="@id" /> </xs:key> <xs:keyref name="PARENT_CATEGORY_ID_REF" refer="ID"> <xs:selector xpath="categories/category" /> <xs:field xpath="@parent-category-id" /> </xs:keyref>
It would be great, if I could add some kind of restriction, that would tell the validator that id cannot == parent-category-id. Any ideas?
If you have an XSD validator that supports version 1.1, you can use an assert after the xs:attribute elements to enforce your restriction:
<xs:assert test="@id != @parent-category-id"/>
Personally I'm having trouble locating a validator that does support 1.1 though. Version 1.0 does not support assert.