Element A in namespace B has an invalid child element C in namespace D
I am getting the following error when validating XML
Element 'Root' in namespace ' http://www.test.com/test ' is not a valid child element of 'Student' in namespace ' http://www.test.com/test '. List of possible elements: "Student".
I cannot post the actual XSD, but I have prepared a small XSD to replicate the problem. Also, I have no control over the XSD because it is provided by the client. The sample XSD looks like this.
<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 am generating XML based on the provided XSD and the data I have. XML is created 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 Element "x" in namespace "xSchema" has an invalid child element "y" in namespace "xSchema". The list of possible elements is "y" , but it indicates that we should remove the use of the prefix
<order>
instead
<os:order>.
But in my case the XML is already generated this way. How can I solve this problem?
I also generated a sample XML using Visual Studio from XSD to see what the difference is. The XML sample that validates only has one line other than
<?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's the difference between adding xmlns = ""? I am looking for alternative solutions and causes of the problem. Please, help.
source to share
This is a great question as it comes down to how xml namespaces work.
The reason you have to set xmlns=""
on the Student element is because the schema indicates elementFormDefault="unqualified"
, which means that any element declared in the Root element is in an "empty" namespace.
Your desired copy:
<Root xmlns="http://www.test.com/test">
<Student>
<StudentName>f</StudentName>
<AdmissionDate>0001-01-01</AdmissionDate>
</Student>
</Root>
will not be checked because Student inherits this namespace from Root and in the schema Student does not belong to this namespace.
So, you have two options, either add xmlns=""
to the Student node (as you already figured out), or use the 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 do elementFormDefault="qualified"
, then your desired instance shape will be valid.
source to share