微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

xml – XSD选择顺序

我想编写一个接受这样的 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: Invalid content was found starting with
element ‘line’. No child element is expected at this point.”

那么……怎么了?我不明白这个消息.什么儿童元素在哪一点?

您的序列定义只有一个孩子,即选择.

这意味着插件只允许一个子节点,尽管它可能是您定义的任何一个元素.

如果删除选择元素,将其内容保留在原位,您将拥有一个固定的元素序列,可以是插件的子元素.

相反,如果删除序列元素,并向choice元素添加一个属性:maxOccurs =“unbounded”,那么它应该以任何顺序验证一个插件元素,该元素包含您指定的任意数量的子元素.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。