Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 259213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:14:43+00:00 2026-05-11T22:14:43+00:00

In my XML I have 6 elements X1, X2, X3, X4, X5 and X6.

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T22:14:43+00:00Added an answer on May 11, 2026 at 10:14 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML Schema Definition file .XSD who's elements are in Spanish. I
I have XML file and the elements/attributes names have : character, how I can
I want to parse xml files that have elements like these: <element>&amp</element> <element>&amp;</element> But
I'm using XmlWriterSettings to write Xml to file. I have elements with only attributes,
I have XML-structure. Context of executing XPath is one of the elements with class
I have a xml that has so many elements and most of that contain
I have an xml document with three child elements repeating in any order. I
I have an XML page with some elements in various languages - Arabic, English,
I have 3 main elements in my XML layout (also see the image): The
I have two major xml elements: courses and CRNs <courses> <course credits=3 courseNum=COMP1950 name=Intermediate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.