XSD 选择序列

发布于 2024-12-04 22:07:05 字数 3813 浏览 0 评论 0原文

我想编写一个接受这样的 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>

所以我希望插件包含集合 {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>

我已经使用这个验证器工具对其进行了测试...

但它说:

“cvc-complex-type.2.4.d:发现无效内容开头 元素“线”。此时不需要子元素。”

那么...出了什么问题?我不明白此消息。什么时候有什么子元素?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

物价感观 2024-12-11 22:07:05

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

这意味着 plugin 只允许有一个子元素,尽管它可以是您定义的任何一个元素。

如果删除 choice 元素,将其内容保留在原处,您将拥有一个固定的元素序列,这些元素可以是 plugin 的子元素。

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

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.

凉栀 2024-12-11 22:07:05

这将允许您在序列中一遍又一遍地重复选择:

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

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

<xs:complexType>
  <xs:sequence>
   <xs:choice maxOccurs="unbounded">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文