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.