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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.