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

时间:2022-09-07 17:18:09

I have an XSD of this form:

我有这种形式的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>

And an XML file that I expect to match it, e.g:

还有一个我希望与之匹配的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>

Unfortunately the XSD says that this is not valid with the following error:

不幸的是,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

As far as I can tell, I've done everything I need to do for the tag to be complete. I've filled it with an unbounded choice of 'a' and 'b' tags. Can anyone see what's gone wrong?

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

To clarify, I want there to be only one 'a' tag under topelement, and underneath that, a mix of 'a' and 'b' tags.

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

3 个解决方案

#1


5  

Before posting this answer, I hadn't observed your own answer .. Anyway I don't want to let my effort/time-spent go waste .. So I won't delete this post .. Along with the same answer I have also .. written some points please go through ..

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

ComplexType aType defines that it always have either <a/> or <b/> as child elements .. It means .. wherever element <a/> appears it must have a child <a/> or <b/> .. which is not true..as per your input XML.

ComplexType aType定义它总是有作为子元素..这意味着......凡 ..根据您的输入XML,这不是真的。

So this the XSD code I have written to overcome the errors, (notice "minOccurs" attribute in the code .. because the absence of which you were getting errors ..)

所以这是我编写的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>

So according to my code .. The Tag <a/> may or may not have any child elements.
If you don't want to change the XSD file .. then your XML must have <a/> tag or <b/> tag as children of <a/> .. something like this :

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

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

Where as this is invalid:

这是无效的:

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


regards: Infant Pro

问候:婴儿临

#2


3  

The error is at the second a not the first the second a needs to have a choice below it.

错误是在第二个,而不是第一个,第二个需要在它下面做出选择。

#3


1  

Worked it out... The error is misleading because it's complaining about the wrong 'a'.

解决了......错误是误导,因为它抱怨错误的'a'。

Rename the top-level 'a' to 'c' and it still complains about 'a' on line 5.

将*'a'重命名为'c',它仍然会在第5行抱怨'a'。

The fix is to add minOccurs=0 to the choice element so that not all 'a' elements need children.

修复是将minOccurs = 0添加到choice元素,以便不是所有'a'元素都需要子元素。

#1


5  

Before posting this answer, I hadn't observed your own answer .. Anyway I don't want to let my effort/time-spent go waste .. So I won't delete this post .. Along with the same answer I have also .. written some points please go through ..

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

ComplexType aType defines that it always have either <a/> or <b/> as child elements .. It means .. wherever element <a/> appears it must have a child <a/> or <b/> .. which is not true..as per your input XML.

ComplexType aType定义它总是有作为子元素..这意味着......凡 ..根据您的输入XML,这不是真的。

So this the XSD code I have written to overcome the errors, (notice "minOccurs" attribute in the code .. because the absence of which you were getting errors ..)

所以这是我编写的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>

So according to my code .. The Tag <a/> may or may not have any child elements.
If you don't want to change the XSD file .. then your XML must have <a/> tag or <b/> tag as children of <a/> .. something like this :

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

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

Where as this is invalid:

这是无效的:

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


regards: Infant Pro

问候:婴儿临

#2


3  

The error is at the second a not the first the second a needs to have a choice below it.

错误是在第二个,而不是第一个,第二个需要在它下面做出选择。

#3


1  

Worked it out... The error is misleading because it's complaining about the wrong 'a'.

解决了......错误是误导,因为它抱怨错误的'a'。

Rename the top-level 'a' to 'c' and it still complains about 'a' on line 5.

将*'a'重命名为'c',它仍然会在第5行抱怨'a'。

The fix is to add minOccurs=0 to the choice element so that not all 'a' elements need children.

修复是将minOccurs = 0添加到choice元素,以便不是所有'a'元素都需要子元素。