XSD选择顺序

时间:2011-09-16 09:19:51

标签: xml xsd sequence choice

我想编写一个接受这样的XML文档的模式:

<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>

所以我希望插件包含set {combobox,checkbox,textbox,label,line,break}的元素。 我写过这个XSD,但这是错误的:

<?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>

我已使用this validator tool ...

对其进行了测试

但它说:

  

“cvc-complex-type.2.4.d:找到无效的内容   元素'线'。此时不会有子元素。“

那么......出了什么问题?我不明白这个消息。什么时候孩子的元素是什么?

2 个答案:

答案 0 :(得分:4)

您的sequence定义只有一个孩子,即choice

这意味着plugin只允许一个孩子,但它可能是您定义的任何一个元素。

如果删除choice元素,将其内容保留在原位,则您将拥有一系列固定元素,可以是plugin的子元素。

相反,如果您删除了序列元素,将其内容保留在原位,将属性添加到choice元素:maxOccurs="unbounded",那么它应该验证plugin元素,包含您指定的任意数量的子项,按任意顺序。

答案 1 :(得分:0)

这将允许您一遍又一遍地重复选择:

<xs:complexType>
  <xs:sequence>
   <xs:choice maxOccurs="unbounded">