The XML I’m trying to validate is as follows:
<root> <element attribute='foo'> <bar/> </element> <element attribute='hello'> <world/> </element> </root>
How can this be validated using a Schema?
Note:
element can only contain bar when attribute=’foo’.
element can only contain world when attribute=’hello’
You can’t do this in XML Schema 1.0. In XML Schema 1.1 you will be able to use the
<xs:assert>element to do it, but I’m guessing you want something that you can use now.You can use Schematron as a second layer of validation that will allow you to test arbitrary XPath assertions about your XML document. There’s a fairly old article about embedding Schematron in XSD that you might find helpful.
You’d do something like:
Or of course you can switch to RELAX NG, which does this in its sleep: