Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD Sequence of Choice

I want to write a schema which accept XML documents like this:

<plugin>
  <label text="blabla" />
  <line />
  <break />
  <textbox name="mytextbox" length="8" required="true" />
  <checkbox name="mycheckbox" checked="true" />
  <combobox name="mycombo">
    <option value="one">Option One</option>
    <option value="two" selected="true">Option One</option>
  </combobox>
  <break />
</plugin>

So I want the plugin to contain elements of set {combobox,checkbox,textbox,label,line,break}. I have written this XSD, but this is wrong:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="plugin">
    <xs:complexType>
      <xs:sequence>
       <xs:choice>
        <xs:element name="line" />
        <xs:element name="break" />
        <xs:element name="label">
          <xs:complexType>
            <xs:attribute name="text" type="xs:string" />
            <xs:attribute name="bold" type="xs:boolean" />
            <xs:attribute name="width" type="xs:positiveInteger" />
          </xs:complexType>
        </xs:element>
        <xs:element name="textbox">
          <xs:complexType>
            <xs:attribute name="name" type="xs:string" />
            <xs:attribute name="width" type="xs:positiveInteger" />
            <xs:attribute name="text" type="xs:string" />
            <xs:attribute name="length" type="xs:positiveInteger" />
            <xs:attribute name="required" type="xs:boolean" />
          </xs:complexType>
        </xs:element>
        <xs:element name="combobox">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="option" nillable="true" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:simpleContent msdata:ColumnName="option_Text" msdata:Ordinal="2">
                    <xs:extension base="xs:string">
                      <xs:attribute name="value" type="xs:string" />
                      <xs:attribute name="selected" type="xs:boolean" />
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="name" type="xs:string" />
            <xs:attribute name="width" type="xs:positiveInteger" />
          </xs:complexType>
        </xs:element>
        <xs:element name="checkbox">
          <xs:complexType>
            <xs:attribute name="name" type="xs:string" />
            <xs:attribute name="checked" type="xs:boolean" />
            <xs:attribute name="text" type="xs:string" />
            <xs:attribute name="width" type="xs:positiveInteger" />
          </xs:complexType>
        </xs:element>
       </xs:choice>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="plugin" />
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

I have tested it with this validator tool...

but it says:

"cvc-complex-type.2.4.d: Invalid content was found starting with element 'line'. No child element is expected at this point."

So...What's wrong? I don't understand this message. What child elements at what point?

like image 371
asdfghjkl Avatar asked Sep 16 '11 09:09

asdfghjkl


People also ask

What is an XSD sequence?

xsd:sequence - "child elements must appear in a sequence. Each child element can occur from 0 to any number of times" (ie, maxOccurs can be 0 or any number or 'unbounded')

Does order matter in XSD?

When you use the xs:sequence indicator, you are saying that the order of the elements matters, and it should follow the order specified. Fro XSD 1.0 implementations, this also means that each child element can occur from 0 to any number of times (unbounded).

What does XSD choice mean?

<xsd:choice> ElementAllows one and only one of the elements contained in the selected group to be present within the containing element. Copy. <choice id = ID maxOccurs= (nonNegativeInteger | unbounded) : 1 minOccurs= nonNegativeInteger : 1 {any attributes with non-schema Namespace}...> Content: (annotation?, (

Does the order of XML attributes matter?

According to the XML specification, the order of attribute specifications in a start-tag or empty-element tag is not significant. Please see the following link. Attributes can appear in any order.


2 Answers

Your sequence definition only has one child, i.e. choice.

This means that plugin is only permitted one child, though it may be any one of the elements you have defined.

If you remove the choice element, leaving its content in situ, you will have a fixed sequence of elements that can be children of plugin.

If, instead, you remove the sequence element, leaving it's content in situ, and add an attribute to the choice element: maxOccurs="unbounded", then it should validate a plugin element with any number of the children you specify, in any order.

like image 129
Paul Butcher Avatar answered Nov 03 '22 04:11

Paul Butcher


This will allow you to repeat the choice over and over in your sequence:

<xs:complexType>
  <xs:sequence>
   <xs:choice maxOccurs="unbounded">
like image 44
Denise Skidmore Avatar answered Nov 03 '22 03:11

Denise Skidmore