gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 01:54:44 24 4
gpt4 key购买 nike

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

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

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

据我所知,我已经完成了使标签完整所需的一切工作。我用“a”和“b”标签的无限选择来填充它。谁能看出哪里出了问题?

澄清一下,我希望在 topelement 下只有一个“a”标签,而在其下方是“a”和“b”标签的混合。

最佳答案

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

复杂类型 aType定义它总是有 <a/><b/>作为子元素 .. 这意味着 .. 无论元素 <a/>看来它必须有一个 child <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>

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

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

无效的地方:

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


问候:婴幼儿专业

关于xml - 为什么 XSD 说我的元素不完整?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2212653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com