我在验证XML时遇到以下错误
名称空间“ http://www.test.com/test ”中的元素“Root”在名称空间“ http://www.test.com/test ”中具有无效的子元素“Student”。 预期可能元素列表:'学生'。
我不能发布实际的XSD,但我准备了一个小的XSD来复制这个问题。 此外,我无法控制XSD,因为它是由客户端提供的。 示例XSD如下所示。
<xs:schema xmlns:stu="http://www.test.com/test" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.test.com/test" elementFormDefault="unqualified" attributeFormDefault="unqualified" version="1.2"> <xs:element name="Root"> <xs:complexType> <xs:sequence> <xs:element name="Student" type="stu:Student" minOccurs="1" maxOccurs="1"> </xs:element> </xs:sequence> </xs:complexType> </xs:element> <xs:simpleType name="Date"> <xs:restriction base="xs:date"> <xs:minInclusive value="0001-01-01"/> <xs:maxInclusive value="9999-12-31"/> <xs:pattern value="\p{Nd}{4}-\p{Nd}{2}-\p{Nd}{2}"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="String100"> <xs:restriction base="xs:string"> <xs:maxLength value="100"/> <xs:minLength value="1"/> </xs:restriction> </xs:simpleType> <xs:complexType name="Student"> <xs:sequence> <xs:element name="StudentName" type="stu:String100" nillable="false"/> <xs:element name="AdmissionDate" type="stu:Date" nillable="false"/> </xs:sequence> </xs:complexType> </xs:schema>我根据提供的XSD和数据生成XML。 XML生成如下。
<?xml version="1.0" encoding="utf-8"?> <Root xmlns="http://www.test.com/test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Student> <StudentName>StudentName1</StudentName> <AdmissionDate>2010-01-01</AdmissionDate> </Student> </Root>我在stackoverflow上检查了这个线程命名空间“xSchema”中的元素“x”在命名空间“xSchema”中具有无效的子元素“y”。 期望的可能元素列表:“y”但它声明我们应该删除前缀使用
<order>代替
<os:order>.但就我而言,XML已经像这样生成了。 我怎样才能克服这个问题?
我还使用XSD中的Visual Studio生成了示例XML,看看有什么区别。 验证的示例XML只有一行不同
<?xml version="1.0" encoding="utf-8"?> <Root xmlns="http://www.test.com/test"> <Student xmlns=""> <!-- JUST THIS xmlns="" ADDED prevents the issue --> <StudentName>StudentName1</StudentName> <AdmissionDate>2010-01-01</AdmissionDate> </Student> </Root>添加xmlns =“”会有什么区别? 我正在寻找替代解决方案和问题的原因。 请帮忙。
I am getting the following error on validating XML
The element 'Root' in namespace 'http://www.test.com/test' has invalid child element 'Student' in namespace 'http://www.test.com/test'. List of possible elements expected: 'Student'.
I cannot post the actual XSD but I have prepared a small XSD to replicate the issue. Also, I have no control on XSD because it is provided by client. The sample XSD looks as follows.
<xs:schema xmlns:stu="http://www.test.com/test" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.test.com/test" elementFormDefault="unqualified" attributeFormDefault="unqualified" version="1.2"> <xs:element name="Root"> <xs:complexType> <xs:sequence> <xs:element name="Student" type="stu:Student" minOccurs="1" maxOccurs="1"> </xs:element> </xs:sequence> </xs:complexType> </xs:element> <xs:simpleType name="Date"> <xs:restriction base="xs:date"> <xs:minInclusive value="0001-01-01"/> <xs:maxInclusive value="9999-12-31"/> <xs:pattern value="\p{Nd}{4}-\p{Nd}{2}-\p{Nd}{2}"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="String100"> <xs:restriction base="xs:string"> <xs:maxLength value="100"/> <xs:minLength value="1"/> </xs:restriction> </xs:simpleType> <xs:complexType name="Student"> <xs:sequence> <xs:element name="StudentName" type="stu:String100" nillable="false"/> <xs:element name="AdmissionDate" type="stu:Date" nillable="false"/> </xs:sequence> </xs:complexType> </xs:schema>I generate the XML based upon provided XSD and data I have. The XML is generated as follows.
<?xml version="1.0" encoding="utf-8"?> <Root xmlns="http://www.test.com/test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Student> <StudentName>StudentName1</StudentName> <AdmissionDate>2010-01-01</AdmissionDate> </Student> </Root>I checked this thread on stackoverflow The element "x" in namespace "xSchema" has invalid child element "y" in namespace "xSchema". List of possible elements expected: "y" but it states that we should remove prefix use
<order>instead of
<os:order>.But in my case the XML is already being generated like that. How can i overcome this issue?
I also generated sample XML using Visual Studio from XSD to see what's the difference. The sample XML which validates has just one line different
<?xml version="1.0" encoding="utf-8"?> <Root xmlns="http://www.test.com/test"> <Student xmlns=""> <!-- JUST THIS xmlns="" ADDED prevents the issue --> <StudentName>StudentName1</StudentName> <AdmissionDate>2010-01-01</AdmissionDate> </Student> </Root>What difference does adding xmlns="" makes? I am looking for alternate solutions and cause of the issue. Please help.
最满意答案
这是一个很好的问题,因为它涉及xml命名空间如何工作的核心。
你必须在Student元素上设置xmlns=""的原因是因为模式指定了elementFormDefault="unqualified" ,这意味着在Root元素下声明的任何元素都驻留在“null”命名空间中。
您想要的实例:
<Root xmlns="http://www.test.com/test"> <Student> <StudentName>f</StudentName> <AdmissionDate>0001-01-01</AdmissionDate> </Student> </Root>将不会验证,因为Student从Root继承了它的命名空间,并且在模式中,Student不属于此命名空间。
因此,您有两个选项,可以将xmlns=""添加到Student节点(如您已经找到的那样),或者在您的实例中使用命名空间前缀:
<x:Root xmlns:x="http://www.test.com/test"> <Student> <StudentName>f</StudentName> <AdmissionDate>0001-01-01</AdmissionDate> </Student> </x:Root>此外,如果您的客户更改架构以使elementFormDefault="qualified"那么您所需的实例表单将是有效的。
This is a great question as it gets to the heart of how xml namespaces work.
The reason you have to set the xmlns="" on the Student element is because the schema specifies elementFormDefault="unqualified", which means that any element declared under the Root element resides in a "null" namespace.
Your desired instance:
<Root xmlns="http://www.test.com/test"> <Student> <StudentName>f</StudentName> <AdmissionDate>0001-01-01</AdmissionDate> </Student> </Root>will not validate because Student inherits it's namespace from Root, and in the schema, Student does not belong to this namespace.
So, you have two options, either add the xmlns="" into the Student node (as you have already found out), or use a namespace prefix in your instance:
<x:Root xmlns:x="http://www.test.com/test"> <Student> <StudentName>f</StudentName> <AdmissionDate>0001-01-01</AdmissionDate> </Student> </x:Root>Also, if your client changes the schema to make elementFormDefault="qualified" then your desired instance form would be valid.
更多推荐
发布评论