Bhushan Bhangale
Bhushan Bhangale

Reputation: 10987

Complex rules in XSD for elements sequence

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?

Upvotes: 1

Views: 2573

Answers (2)

George Bina
George Bina

Reputation: 1161

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 version="1.0" encoding="UTF-8"?>
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
  <pattern>
    <rule context="X1">
      <assert test="following-sibling::*[1][self::X2]">
        X1 must be followed by X2
      </assert>
    </rule>
    <rule context="X2">
      <assert test="following-sibling::*[1][self::X3]">
        X2 must be followed by X3
      </assert>
    </rule>
    <rule context="X3">
      <assert test="following-sibling::*[1][self::X4]">
        X3 must be followed by X4
      </assert>
    </rule>
    <rule context="X4">
      <assert test="following-sibling::*[1][self::X2 or self::X3 or self::X4 or self::X5]">
        X4 must be followed by X2, X3, X4 or X5
      </assert>
    </rule>
    <rule context="X5">
      <assert test="following-sibling::*[1][self::X6]">
        X5 must be followed by X6
      </assert>
    </rule>
    <rule context="X6">
      <assert test="not(following-sibling::*) or following-sibling::*[1][self::X2 or self::X3 or self::X4 or self::X5 or self::X6]">
        X6 can be followed by X2, X3, X4, X5 or X6
      </assert>
    </rule>
  </pattern>
</schema>

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.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:sch="http://purl.oclc.org/dsdl/schematron">
  <xs:annotation>
    <xs:appinfo>
      <sch:title>Sample embedded Schematron rules</sch:title>
    </xs:appinfo>
  </xs:annotation>
  <xs:element name="test">
    <xs:annotation>
      <xs:appinfo>
        <sch:pattern>
          <sch:rule context="X1">
            <sch:assert test="following-sibling::*[1][self::X2]"> X1 must be followed by X2 </sch:assert>
          </sch:rule>
          <sch:rule context="X2">
            <sch:assert test="following-sibling::*[1][self::X3]"> X2 must be followed by X3 </sch:assert>
          </sch:rule>
          <sch:rule context="X3">
            <sch:assert test="following-sibling::*[1][self::X4]"> X3 must be followed by X4 </sch:assert>
          </sch:rule>
          <sch:rule context="X4">
            <sch:assert test="following-sibling::*[1][self::X2 or self::X3 or self::X4 or self::X5]"> X4
              must be followed by X2, X3, X4 or X5 </sch:assert>
          </sch:rule>
          <sch:rule context="X5">
            <sch:assert test="following-sibling::*[1][self::X6]"> X5 must be followed by X6 </sch:assert>
          </sch:rule>
          <sch:rule context="X6">
            <sch:assert
              test="not(following-sibling::*) or following-sibling::*[1][self::X2 or self::X3 or self::X4 or self::X5 or self::X6]"
              > X6 can be followed by X2, X3, X4, X5 or X6 </sch:assert>
          </sch:rule>
        </sch:pattern>
      </xs:appinfo>
    </xs:annotation>
    <xs:complexType>
      <xs:choice maxOccurs="unbounded">
        <xs:element ref="X1"/>
        <xs:element ref="X2"/>
        <xs:element ref="X3"/>
        <xs:element ref="X4"/>
        <xs:element ref="X5"/>
        <xs:element ref="X6"/>
      </xs:choice>
    </xs:complexType>
  </xs:element>

  <xs:element name="X1"/>
  <xs:element name="X2"/>
  <xs:element name="X3"/>
  <xs:element name="X4"/>
  <xs:element name="X5"/>
  <xs:element name="X6"/>
</xs:schema>

An instance document can look like below:

<?oxygen SCHSchema="test.xsd"?>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="test.xsd">
  <X1/>
  <X2/>
  <X3/>
  <X4/>
  <X4/>
  <X3/>
  <X4/>
  <X5/>
  <X6/>
  <X4/>
  <X5/>
  <X6/>
</test>

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.

Upvotes: 4

Conspicuous Compiler
Conspicuous Compiler

Reputation: 6469

Anyhow, here's a stab at a solution, presuming X6 can be the terminal item and "OverallSequence" is the base element.


<xs:complexType name="OverallSequence">
  <xs:group  ref="firstItem" />
</xs:complexType>

<xs:group name="sixthChoice"> <xs:choice> <xs:group ref="secondItem" /> <xs:group ref="thirdItem" /> <xs:group ref="fourthItem" /> <xs:group ref="fifthItem" /> <xs:group ref="sixthItem" /> </xs:choice> </xs:group>

<xs:group name="sixthItem"> <xs:sequence> <xs:element name="X6" type="xs:string" /> <xs:group ref="sixthChoice" minOccurs="0" > </xs:sequence> </xs:group>

<xs:group name="fifthItem"> <xs:sequence> <xs:element name="X5" type="xs:string" /> <xs:group ref="sixthItem" /> </xs:sequence> </xs:group>

<xs:group name="fourthChoice"> <xs:choice> <xs:group ref="secondItem" /> <xs:group ref="thirdItem" /> <xs:group ref="fourthItem" /> </xs:choice> </xs:group>

<xs:group name="fourthItem" /> <xs:sequence> <xs:element name="X4" type="xs:string" /> <xs:group ref="fourthChoice" minOccurs="0" > <xs:group ref="fifthItem" /> </xs:sequence> </xs:group>

<xs:group name="thirdItem" /> <xs:sequence> <xs:element name="X3" type="xs:string" /> <xs:group ref="fourthItem"> </xs:sequence> </xs:group>

<xs:group name="secondItem" /> <xs:sequence> <xs:element name="X2" type="xs:string" /> <xs:group ref="thirdItem"> </xs:sequence> </xs:group>

<xs:group name="firstItem" /> <xs:sequence> <xs:element name="X1" type="xs:string" /> <xs:group ref="secondItem"> </xs:sequence> </xs:group>

Upvotes: 1

Related Questions