当我试图根据下面的XSD验证下面的XML时,我会得到以下错误:
2.4.a:找到以元素>'personal‘开头的无效内容。一个{个人}的期望。
XML
<main xmlns = "http://www.example.com"
xmlns:xsi = "https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "main.xsd">
<personal>
<full-name>John Smith</full-name>
<contact>
<street-address>12345 Example Street</street-address>
<city>Somewhere</city>
<state>EX</state>
<postal-code>111 111</postal-code>
<phone>123 456 7890</phone>
<email>myemail@example.com</email>
</contact>
</personal>
</main> XSD
<xsd:schema xmlns:xsd = "http://www.w3.org/2001/XMLSchema"
targetNamespace = "http://www.example.com"
xmlns = "http://www.example.com">
<xsd:element name = "main" type = "main-type"/>
<xsd:complexType name = "main-type">
<xsd:all>
<xsd:element name = "personal" type = "personal-type"/>
</xsd:all>
</xsd:complexType>
<xsd:complexType name = "personal-type">
<xsd:all>
<xsd:element name = "full-name" type = "xsd:string"
minOccurs = "1"/>
<xsd:element name = "contact" type = "contact-type"
minOccurs = "1"/>
</xsd:all>
</xsd:complexType>
<!--Different xsd:strings for contact information in contact-type-->
<xsd:complexType name = "contact-type">
<xsd:all>
<xsd:element name = "street-address" type = "xsd:string"/>
<xsd:element name = "city" type = "xsd:string"/>
<xsd:element name = "state" type = "xsd:string"/>
<xsd:element name = "postal-code" type = "xsd:string"/>
<xsd:element name = "phone" type = "xsd:string"/>
<xsd:element name = "email" type = "xsd:string"/>
</xsd:all>
</xsd:complexType>
</xsd:schema>有什么问题,我该怎么解决呢?
发布于 2017-10-15 04:23:22
发布的XML在发布错误消息之前有两个初步问题:
现在,您发布的XML和XSD实际上将处于展示您发布的问题的状态:
错误main.xml:4:13: cvc-complex- with .2.4.a:找到以元素'personal‘开头的无效内容。“{personal}”之一是预期的。
Explanation:这个错误告诉您,根据XSD,personal不会出现在任何名称空间中;One of '{personal}' is expected中的{和}表明了这一点。
您可能会认为,由于XSD声明了targetNamespace="http://www.example.com",因此它的所有组件都被放置在http://www.example.com命名空间中。但是,对于本地声明的组件来说,情况并非如此,除非您设置了elementFormDefault="qualified" --默认为unqualified。
默认情况下,本地声明的元素不存在命名空间。
因此,做最后一个更改:添加
elementFormDefault="qualified"到xsd:schema元素,然后您的XML对XSD有效。
https://stackoverflow.com/questions/46750490
复制相似问题