views:

380

answers:

2

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?

+1  A: 
Conspicuous Compiler
This is not working, its complaining about circular reference. As you can see fourthItem includes fourthChoice and then fourthChoice again includes fourthItem. Same is true for others as well.
Bhushan
+2  A: 

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"&gt;
  <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"&gt;
  <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.

George Bina
Where to add this pattern in my current xsd? I added the pattern element under my schema and define xml name space but it gives error or root element missing.
Bhushan
See the follow up in my answer for a complete working sample.
George Bina