Question

I've got a RelaxNG schema that looks pretty much like this:

<grammar xmlns="http://relaxng.org/ns/structure/1.0">
  <start>
    <element name="xml">
      <zeroOrMore>
        <element name="multi">
          <data type="string"/>
        </element>
      </zeroOrMore>
      <optional>
        <element name="optional">
          <data type="string"/>
        </element>
      </optional>
    </element>
  </start>
</grammar>

I want to allow 0-N multi tags, but 0-1 optional. That works fine as long as the multis come before the optional, but since relative order matters in RNG, it fails if they come afterward:

Valid:

<xml>
  <optional/>
</xml>

<xml>
  <multi/>
  <multi/>
  <optional/>
</xml>

Invalid:

<xml>
  <optional/>
  <multi/>
  <multi/>
</xml>

So how can I allow arbitrary order but retain the constraints? I tried wrapping the whole thing in a <zeroOrMore><choice> block, and that allows arbitrary order, but also lets any number of optional tags through.

Was it helpful?

Solution

If I understand your question correctly, what you are trying to achieve is accomplished by use of the <interleave/> pattern:

<?xml version="1.0" encoding="UTF-8"?>
<grammar xmlns="http://relaxng.org/ns/structure/1.0">
    <start>
        <element name="xml">
            <interleave>
                <zeroOrMore>
                    <element name="multi">
                        <data type="string"/>
                    </element>
                </zeroOrMore>
                <optional>
                    <element name="optional">
                        <data type="string"/>
                    </element>
                </optional>                
            </interleave>
        </element>
    </start>
</grammar>

This will validate any sequence or zero or one optional elements and zero or more multi elements including your examples.

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