为什么XSD说我的元素不完整?

izb*_*izb 5 xml xsd

我有这种形式的XSD:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.org/example"
    xmlns:tns="http://www.example.org/example" elementFormDefault="qualified">

    <complexType name="bType">
    </complexType>

    <complexType name="aType">
        <choice maxOccurs="unbounded">
            <element name="a" type="tns:aType" />
            <element name="b" type="tns:bType" />
        </choice>
    </complexType>

    <element name="topelement">
        <complexType>
            <sequence>
                <element name="a" type="tns:aType" maxOccurs="1" />
            </sequence>
        </complexType>
    </element>
</schema>
Run Code Online (Sandbox Code Playgroud)

我希望与之匹配的XML文件,例如:

<?xml version="1.0" encoding="UTF-8"?>
<topelement xmlns="http://www.example.org/example"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.example.org/example example.xsd ">
  <a> <!-- Error on this line. -->
    <a/>
    <b/>
    <b/>
    <a/>
  </a>
</topelement>
Run Code Online (Sandbox Code Playgroud)

不幸的是,XSD说这对以下错误无效:

cvc-complex-type.2.4.b: The content of element 'a' is not complete. One of '{"http://www.example.org/example":a, "http://www.example.org/example":b}' is expected.  example.xml line 5
Run Code Online (Sandbox Code Playgroud)

据我所知,我已经完成了标签完成所需的一切.我已经用无限选择的'a'和'b'标签填充它.有人能看出出了什么问题吗?

为了澄清,我希望在topelement下只有一个'a'标签,在其下面,混合了'a'和'b'标签.

Inf*_*nd' 5

在发布这个答案之前,我没有观察到你自己的答案..无论如何我不想让我的努力/时间花费浪费..所以我不会删除这篇文章..同样的答案我有还..写了一些要点,请通过..

ComplexType aType定义它总是具有<a/><b/>作为子元素.它意味着..无论元素<a/>出现在哪里,它必须有一个子<a/><b/>..根据您的输入XML,它不是真的.

所以这是我编写的XSD代码来克服错误,(注意代码中的"minOccurs"属性..因为没有你得到错误..)

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.org/example"
    xmlns:tns="http://www.example.org/example" elementFormDefault="qualified">
  <element name="topelement">
    <complexType>
      <sequence>
        <element name="a" type="tns:aType" minOccurs="0" maxOccurs="1" />
      </sequence>
    </complexType>
  </element>


  <complexType name="bType">
  </complexType>

  <complexType name="aType">
    <sequence>
      <choice maxOccurs="unbounded">
        <element name="a" type="tns:aType" minOccurs="0"/>
        <element name="b" type="tns:bType" minOccurs="0"/>
      </choice>
    </sequence>
  </complexType>
</schema>
Run Code Online (Sandbox Code Playgroud)

所以根据我的代码..标签<a/>可能有也可能没有任何子元素.
如果您不想更改XSD文件..那么您的XML必须具有<a/>标记或<b/>标记作为子代<a/>...如下所示:

<topelement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.example.org/example" xsi:schemaLocation="http://www.example.org/example example.xsd">
    <a>
    <a>
      <b/>
    </a>
    <b/>
    <b/>
    <a>
      <a>
       <b/>
      </a>
      <b/>
    </a>
</topelement>
Run Code Online (Sandbox Code Playgroud)

这是无效的:

<topelement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.example.org/example" xsi:schemaLocation="http://www.example.org/example example.xsd">
    <a>
    <a>
      <b/>
    </a>
    <a/><!--this is wrong-->
    <b/>
    </a>
</topelement>
Run Code Online (Sandbox Code Playgroud)


问候:婴儿临


izb*_*izb 1

解决了...该错误具有误导性,因为它抱怨错误的“a”。

将顶级“a”重命名为“c”,它仍然在第 5 行抱怨“a”。

解决方法是将 minOccurs=0 添加到 choice 元素,以便并非所有 'a' 元素都需要子元素。