xml 元素不能有字符 [children],因为该类型的内容类型是 element-only
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34493270/
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
Element cannot have character [children], because the type's content type is element-only
提问by Toshan
I tried validating my XML file with a XSD file but I get the following error message:
我尝试使用 XSD 文件验证我的 XML 文件,但收到以下错误消息:
[Error]: cvc-complex-type.2.3: Element 'paragraph' cannot have character [children], because the type's content type is element-only
[错误]:cvc-complex-type.2.3:元素“段落”不能有字符[孩子],因为该类型的内容类型是仅元素
Which (if i am understanding it correctly) means a complex type cannot have a combination of simple and complex typed elements as children.
这(如果我理解正确的话)意味着复杂类型不能将简单和复杂类型元素的组合作为子元素。
But how do I fix this? I'm still kinda new to this, so the solution could be simple?
但是我该如何解决这个问题?我对此还是有点陌生,所以解决方案可能很简单?
My code is shown below:
我的代码如下所示:
XSD:
XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="biography">
<xs:complexType>
<xs:sequence>
<xs:element name="paragraph" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name">
<xs:complexType>
<xs:sequence>
<xs:element name="first_name" type="xs:string" minOccurs="0"></xs:element>
<xs:element name="last_name" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="emphasize" maxOccurs="unbounded" minOccurs="0" type="xs:string"></xs:element>
<xs:element name="profession" maxOccurs="unbounded" minOccurs="0" type="xs:string"></xs:element>
<xs:element name="date" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="month" type="xs:string"></xs:element>
<xs:element name="day" type="xs:int"></xs:element>
<xs:element name="year" type="xs:int"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="definition" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="term" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML:
XML:
<biography>
<paragraph>
<name><first_name>Alex</first_name> <last_name>Turing</last_name></name>
was one of the first people to truly deserve the name <emphasize>computer
scientist</emphasize>. Although his contributions to the fields are too
numerous to lst, his best-known are the famous <emphasize>Turing
Test</emphasize> and <emphasize>Turing Machine</emphasize>.
</paragraph>
<definition>The <term>Turing Test</term> is to this day the standard test
for determining whether a computer is truly intelligent. This test yet
has to be passed.</definition>
<definition>A <term>Turing Machine</term> is an abstract finite state
automaton with infinite memory that can be proven equivalent to any other
finite state automaton with arbitrarily large memory. Thus what is true
for one Turing machine is true for all Turing machines no matter how
implemented.</definition>
<paragraph>
<name><last_name>Turing</last_name></name> was also an accomplished
<profession>mathematician</profession> and
<profession>cryptographer</profession>. His assistance was crucial in
helping the Allies decode the German Enigma cipher. He committed suicide
on <date><month>June</month> <day>7</day>, <year>1954</year></date>
after being convicted of homosexuality and forced to take female hormone
injections.
</paragraph>
</biography>
采纳答案by kjhughes
This is precisely the purpose of mixedcontent:
这正是混合内容的目的:
<xs:element name="paragraph" maxOccurs="unbounded">
<xs:complexType mixed="true">
And:
和:
<xs:element name="definition" maxOccurs="unbounded">
<xs:complexType mixed="true">
Note that you also probably want paragraphand definitionto be in a xs:choice macOccurs="unbounded"given your XML.
请注意,您可能还希望paragraph并definition在xs:choice macOccurs="unbounded"给定的 XML 中。
Here's your XSD updated with all changes needed so that your XML will be valid:
这是您的 XSD 更新了所需的所有更改,以便您的 XML 有效:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="biography">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="paragraph">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="name">
<xs:complexType>
<xs:sequence>
<xs:element name="first_name" type="xs:string" minOccurs="0"></xs:element>
<xs:element name="last_name" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="emphasize" maxOccurs="unbounded" minOccurs="0" type="xs:string"></xs:element>
<xs:element name="profession" maxOccurs="unbounded" minOccurs="0" type="xs:string"></xs:element>
<xs:element name="date" minOccurs="0">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="month" type="xs:string"></xs:element>
<xs:element name="day" type="xs:int"></xs:element>
<xs:element name="year" type="xs:int"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="definition">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="term" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
回答by Varun M
For me This error happen because of the strange character in the xml When I compared two files with similar data in "BeyondCompare" tool I found unknown character which is not visible. Open faulty file in "Visual Studio Code" it showed me some special characters.
对我来说,发生此错误是因为 xml 中的奇怪字符当我在“BeyondCompare”工具中比较两个具有相似数据的文件时,我发现了不可见的未知字符。在“Visual Studio Code”中打开错误文件,它向我展示了一些特殊字符。
When I copy pasted the string in online tools to find special characters, I found that is a special character ().
当我在在线工具中复制粘贴字符串以查找特殊字符时,我发现这是一个特殊字符()。
Because of this character, I got above error. It took two weeks to find solution.
由于这个角色,我得到了上述错误。花了两周时间才找到解决办法。

