4

I'm still a bit new to XML Schema, and I'm trying to do something that would look like this in Relax NG Compact:

test = element test{
element A {text},
element B {text},
(element C {text}? &
element D {text}?)
}

Which means that in the test element contains A, then B, then in any order C and D, which are both optional.

The way I see it, I should be able to simply put

<xs:element name="test">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="A"/>
            <xs:element name="B"/>
            <xs:all>
                <xs:element name="C"/>
                <xs:element name="D"/>
            </xs:all> 
        </xs:sequence>
    </xs:complexType>
</xs:element>

But it won't let me put an <xs:all> inside an <xs:sequence>. Saying

s4s-elt-must-match.1: The content of 'sequence' must match (annotation?, (element | group | choice | sequence | any)*). A problem was found starting at: all.

So I tried taking <xs:all> out of <xs:sequence> like so:

<xs:element name="test">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="A" />
                <xs:element name="B"/>
            </xs:sequence>
            <xs:all>
                <xs:element name="C"/>
                <xs:element name="D"/>
            </xs:all> 
        </xs:complexType>
    </xs:element>

But now it still doesn't work, saying

s4s-elt-invalid-content.1: The content of '#AnonType_test' is invalid. Element 'all' is invalid, misplaced, or occurs too often

So I'm confused because it seems so simple, yet I'm not figuring out how to do this.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Wilzy X
  • 43
  • 2
  • "Why?" questions are always difficult. Are you really asking why the authors of the spec chose to impose this restriction? Or are you asking for more precise information about the nature of the restriction? Or are you asking how to get around the restriction? – Michael Kay Jun 28 '17 at 09:35
  • You are right, "why?" questions aren't the best, but it was meant as a 2 parts question. "First, am I only missing some easy fix? If not, why wouldn't xsd allow us to do such a simple thing? " I understand that it's not a bad idea to impose ordering, but I was wondering how can it be so simple in Relax NG Compact, yet impossible in Xml Schema. I heard there's a way to convert rnc to xsd, so I wonder how it would translate the use of the '&' char inside a sequence. – Wilzy X Jul 04 '17 at 22:35
  • Some of the restrictions in XSD are there because of deliberate design choices, notably (a) no backtracking, and (b) unambiguous type assignment to elements (something RNG does not offer). Some are there because the WG was a bit paternalistic: they wanted to enforce what they considered good document design. Some are there because it was a very large committee that didn't always agree on everything, so they made compromises that no-one was happy with. – Michael Kay Jul 05 '17 at 09:55

2 Answers2

1

Your confusion is understandable. The problem is that XSD design is irregular, and irregular designs often violate our expectations.

Here's a work-around, which is unfortunately more verbose and also impractical for larger numbers of elements to be permuted:

  <xs:element name="test">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="A"/>
        <xs:element name="B"/>
        <xs:choice minOccurs="0">
          <xs:sequence>
            <xs:element name="C"/>
            <xs:element name="D" minOccurs="0"/>
          </xs:sequence>
          <xs:sequence>
            <xs:element name="D"/>
            <xs:element name="C" minOccurs="0"/>
          </xs:sequence>
        </xs:choice>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

Another workaround is to impose an ordering; allowing any order often isn't important in practice.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
0

One of the reasons for the design, in my view, is that allowing mixing order-independent (all) and order-dependent (sequence, choice, ...) model groups would increase the complexity of XML Schema validation in terms of algorithms, and would put more burden on implementors.

But more than that, in some cases involving, e.g., optional elements, it could also lead to ambiguities when matching elements to particles (i.e., element declarations inside model groups), introducing non-determinism or implementation-dependence, which is not desirable for interoperability.

The benefits of such restrictions overweigh their costs, overall. Most XML Schema users use sequence and choice and force an order (see kjhughes's example).

Relax NG (which I am not familiar with) may use a different model for describing the content of elements, and this model is probably compatible with different design decisions.

Ghislain Fourny
  • 6,971
  • 1
  • 30
  • 37