为什么 XSD 验证期望 {element} 带有大括号而不只是元素?
Why does XSD validation expect {element} with braces rather than just element?
当我尝试根据以下 XSD 验证以下 XML 时,出现以下错误:
cvc-complex-type.2.4.a: Invalid content was found starting with element >'personal'. One of '{personal} expected.
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>
有什么问题,我该如何解决?
您发布的 XML 在您发布的错误消息之前有两个初步问题:
改变
xmlns:xsi = "https://www.w3.org/2001/XMLSchema-instance"
至
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
改变
xsi:schemaLocation = "main.xsd"
至
xsi:schemaLocation = "http://www.example.com main.xsd"
现在,您发布的 XML 和 XSD 实际上将处于显示您发布的问题的状态:
[Error] main.xml:4:13: cvc-complex-type.2.4.a: Invalid content was
found starting with element 'personal'. One of '{personal}' is
expected.
解释: 根据您的 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 有效。
另请参阅 this answer about what elementFormDefault means。
当我尝试根据以下 XSD 验证以下 XML 时,出现以下错误:
cvc-complex-type.2.4.a: Invalid content was found starting with element >'personal'. One of '{personal} expected.
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>
有什么问题,我该如何解决?
您发布的 XML 在您发布的错误消息之前有两个初步问题:
改变
xmlns:xsi = "https://www.w3.org/2001/XMLSchema-instance"
至
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
改变
xsi:schemaLocation = "main.xsd"
至
xsi:schemaLocation = "http://www.example.com main.xsd"
现在,您发布的 XML 和 XSD 实际上将处于显示您发布的问题的状态:
[Error] main.xml:4:13: cvc-complex-type.2.4.a: Invalid content was found starting with element 'personal'. One of '{personal}' is expected.
解释: 根据您的 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 有效。
另请参阅 this answer about what elementFormDefault means。