XML 验证:“此时不需要子元素”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21341425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML Validation: "No Child Element Is Expected At This Point"
提问by goldfrapp04
I'm trying to develop an XSD grammar according to a given XML file. The given XML file itemList.xmlis shown as below.
我正在尝试根据给定的 XML 文件开发 XSD 语法。给定的 XML 文件itemList.xml如下所示。
<?xml version="1.0" encoding = "utf-8"?>
<itemList
xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com itemList.xsd" >
<item>spoon</item>
<item>knife</item>
<item>fork</item>
<item>cup</item>
</itemList>
The itemList.xsdfile that I developed is shown as below.
我开发的itemList.xsd文件如下所示。
<schema
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:co="http://www.w3schools.com"
targetNamespace="http://www.w3schools.com"
elementFormDefault="qualified">
<simpleType name="itemType">
<restriction base="string"/>
</simpleType>
<complexType name="itemListType">
<sequence>
<element name="item" type="co:itemType"/>
</sequence>
</complexType>
<element name="itemList" type="co:itemListType"/>
</schema>
When I validate the XML against the XSD using this XML validator, I get the error
当我使用此 XML 验证器针对 XSD验证 XML 时,出现错误
Cvc-complex-type.2.4.d: Invalid Content Was Found Starting With Element 'item'. No Child Element Is Expected At This Point.. Line '6', Column '12'.
It seems that I should rewrite my complexTypein itemList.xsd, but I'm not sure what to do. Many thanks to whoever could help.
似乎我应该complexType在itemList.xsd 中重写我的,但我不知道该怎么做。非常感谢任何可以提供帮助的人。
回答by Petru Gardea
Your itemList is currently made of exactly one item; that is because the default particle cardinality is 1 (minOccurs = maxOccurs = 1).
您的 itemList 当前仅由一件商品组成;这是因为默认粒子基数是 1 (minOccurs = maxOccurs = 1)。
If you wish more than one, then you need to add maxOccurs attribute with the appropriate number; for unlimited, use maxOccurs="unbounded"... like so:
如果您希望不止一个,那么您需要添加具有适当数量的 maxOccurs 属性;对于无限制,使用 maxOccurs="unbounded"... 像这样:
<element name="item" type="co:itemType" maxOccurs="unbounded"/>
回答by Dave Richardson
In my case I got this message because the ordering of the fields in my XML did not match those in my XSD, I had erroneously reversed the order of the last two fields.
就我而言,我收到此消息是因为 XML 中字段的顺序与 XSD 中的字段顺序不匹配,我错误地颠倒了最后两个字段的顺序。
While this is not the situation in the question, I thought it may help others who are drawn to this question by the title (doubtless I will reference this question again in the future when I have forgotten what I have just learned).
虽然这不是问题中的情况,但我认为它可能会帮助其他被标题吸引到这个问题的人(毫无疑问,当我忘记刚刚学到的东西时,我会再次引用这个问题)。

