Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD "one or both" choice construct leads to ambiguous content model

Tags:

xsd

I am trying to make a simple XSD choice construct allowing either one or both of two referenced elements, but not none. The construct is similar to below but I keep getting an ambiguity error. What am I missing?

<xs:schema xmlns:xs="...">
  <xs:element name="Number" type="xs:integer"/>
  <xs:element name="Text" type="xs:string"/>
  <xs:element name="RootStructure">
    <xs:complexType>
      <xs:sequence>
        <xs:choice>
          <xs:sequence>
            <xs:element ref="Number"/>
            <xs:element ref="Text"/>
          </xs:sequence>
          <xs:element ref="Number"/>
          <xs:element ref="Text"/>
        </xs:choice>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
like image 517
lox Avatar asked Mar 03 '10 15:03

lox


People also ask

What does XSD choice mean?

The XSD choice element extends the XML Schema definition. It provides a single or multiple choices of content elements in an arbitrary order. We describes how to use the choice element within XSDs and how to access the values in a JSP with EL.

What is the difference between xs and XSD?

For brevity, the text and examples in this specification use the prefix xs: to stand for this namespace; in practice, any prefix can be used. in the end xs or xsd are only prefixes. XSD is used for example more by Microsoft schemas. The important is how you declare the namespace.

Can XSD have multiple root elements?

While a properly formed XML file can only have a single root element, an XSD or DTD file can contain multiple roots.

How to model XSD complex types with choice model group?

In order to model XSD complex types with a choice model group surrounding some elements, your have to decide which elements your want inside a choice model group. 1. Create a UML Package to hold the classes

What is the choice element in XSDs?

The XSD choice element extends the XML Schema definition. It provides a single or multiple choices of content elements in an arbitrary order. We describes how to use the choice element within XSDs and how to access the values in a JSP with EL. The <xsd:choice> -node can be used in the same way as a <xsd:sequence> to describe an OpenCms type.

How to describe an OpenCms type using <XSD choice> -node?

The <xsd:choice> -node can be used in the same way as a <xsd:sequence> to describe an OpenCms type. It has to be defined in XML schema definition of a nested XML content. The element in the root schema has to be optional. Add attribute minOccurs="0" on the element to make it optional. Root XML Schema Definition:

What is choice element in XML Schema?

XML Schema choice element allows only one of the elements contained in the <choice> declaration to be present within the containing element. Parent elements: group, choice, sequence, complexType, restriction (both simpleContent and complexContent), extension (both simpleContent and complexContent)


2 Answers

The usual way to do it is this:

<xs:schema xmlns:xs="...">
  <xs:element name="Number" type="xs:integer"/>
  <xs:element name="Text" type="xs:string"/>
  <xs:element name="RootStructure">
    <xs:complexType>
      <xs:sequence>
        <xs:choice>
          <xs:sequence>
            <xs:element ref="Number"/>
            <xs:element ref="Text" minOccurs="0"/>
          </xs:sequence>
          <xs:element ref="Text"/>
        </xs:choice>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
like image 60
xcut Avatar answered Sep 24 '22 13:09

xcut


Some additional hint, if you have multiple elements linked and you want one bundle of elements or the other bundle, or both, you can do it like this:

<xsd:complexType name="ComplexTypeName">
    <xsd:choice>
        <xsd:sequence>
            <xsd:element name="theElement" />
            <xsd:element name="theElementIsFlagged" />
            <xsd:choice>
                <xsd:sequence>
<!-- note the empty sequence block -->
                </xsd:sequence>
                <xsd:sequence>
                    <xsd:element name="theOtherElement" />
                    <xsd:element name="theOtherElementIsFlagged" />
                </xsd:sequence>
            </xsd:choice>
        </xsd:sequence>
        <xsd:sequence>
            <xsd:element name="theOtherElement" />
            <xsd:element name="theOtherElementIsFlagged" />
        </xsd:sequence>
    </xsd:choice>
</xsd:complexType>

Just in case some of you bump into the same issue!!

like image 44
krystine.e Avatar answered Sep 21 '22 13:09

krystine.e