Question

I have an XML file that looks like the following:

<root>
    <node1></node1>
    <node2></node2>
    <node3></node3>
    <line>
       <line1></line1>
       <line2></line2>
       <line3></line3>
    </line>
    <line>
       <line1></line1>
       <line2></line2>
       <line3></line3>
    </line>
</root>

Here are my restrictions:

  • <node1>,<node2>,<node3> must each occur exactly once and may occur in any order
  • <node1>,<node2>,<node3> must all occur prior to the beginning of the <line> elements
  • There are an arbitrary number of <line> elements, but there must be at least one
  • <line1>,<line2>,<line3> elements in <line> must each occur exactly once and may occur in any order
  • I have no control over the XML. It's being given to me by some third-party software.
  • I have to use XSD 1.0.
  • This is a simplified example. I'm looking at 20 or so unique <node#>s, so the solution has to scale well.

I'm trying to design an XSD file to validate this XML, but to no avail. The problem is that I'm essentially looking at an <xs:all> block (for my <node#>s) followed by an <xs:sequence> or <xs:choice> block with maxOccurs="unbounded" (for my <line>s), but it seems like there's no valid way to do this with XSD 1.0.

Was it helpful?

Solution

There are only six possible sequences of (1,2,3) appearing once each in any order, so it's feasible to enumerate them all. Of course this doesn't scale but it might work for you. That would be

(1((2,3)|(3,2)) | 2((1,3)|(3,1)) | 3((1,2)|(2,1))

using "," for sequence and "|" for choice.

Your only other options are not XSD 1.0. E.g. XSD 1.1 or schematron.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top