In my XML I have 6 elements X1, X2, X3, X4, X5 and X6. The rules are as follows –
X1 must be followed by X2
X2 must be followed by X3
X3 must be followed by X4
X4 must be followed by X2, X3, X4 or X5
X5 must be followed by X6
X6 can be followed by X2, X3, X4, X5 or X6
I am trying to define an XSD for this. Is it possible? I tried few things using sequence and minOccur and maxOccur but not working. Any ideas which I can try?
XML Schema 1.0 cannot handle this case.
Schematron, on the other hand, translates your constraints directly, see a working/tested Schematron schema below:
XML Schema 1.1 will add assertion support but that has some limitations on the scope the assertion XPath applies to – you will need to place the assertions on the parent element of X1..X6.
Note also that the Schematron rules can stay embedded in XML Schema inside xs:annotation/xs:appinfo and you should be able to validate against both the XML Schema and against the embedded Schematron rules.
Follow up after the OP comment
An XML Schema embedding the above Schematron rules is below. This is a sample that defines an element test that can contain the X1..X6.
An instance document can look like below:
The above is tested with oXygen XML Editor. The instance associates the XML Schema also with the processing instruction to tell oXygen that the schema contains embedded Schematron rules. The validation is performed against both the XML Schema and against the Schematron rules.