Let's suppose I'm writing a schema for an XML document wherein there's a document describing pieces on a linear game board:
<piece length="5" position="4" />
where the attributes length
and position
are both non-negative integers greater than 0. Now, there's a clear relation between length
and position
that further restricts the set of meaningful values. That is to say, position
can never be more than length
e.g. the XML fragment
<piece length="5" position="6" />
is clearly nonsensical.
My question is whether there is any way to enforce this restriction using an XML Schema? If not in the W3C XML Schema language, what about RELAX NG? Supposing that what I am asking for is not possible in the unrestricted case (i.e. for any positive integer) what about for a constrained, finite set of possibilities? That is, suppose we say that length
can never be more than four. Given the aforementioned restriction, this leaves merely 4 possible values for length
. Would it then be possible to accomplish what I desire, even if it means manually?
To express such constraints you need XSD 1.1 assertions, for example
<xsl:assert test="@position lt @length"/>
XSD 1.1 is implemented in Saxon, Xerces, and Altova.
The alternative is to use Schematron.