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>
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.
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.
While a properly formed XML file can only have a single root element, an XSD or DTD file can contain multiple roots.
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
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.
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:
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)
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>
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!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With