Why my test xml is failing with very simple XSD Schema?
-
26-09-2019 - |
Question
I am a bit novice in xml schema. I would be grateful if somebody help me out to understand why my xml is not being validated with the schema:
Here is my Schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/testSchema" xmlns="http://www.example.org/testSchema">
<xs:element name="Employee">
<xs:complexType>
<xs:sequence>
<xs:element name="Name">
<xs:complexType>
<xs:sequence>
<xs:element name="FirstName" />
<xs:element name="LastName" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Here is my test xml:
<?xml version="1.0" encoding="UTF-8"?>
<Employee xmlns="http://www.example.org/testSchema">
<Name>
<FirstName>John</FirstName>
<LastName>Smith</LastName>
</Name>
</Employee>
I am getting following error by Eclipse xml editor/validator:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'Name'. One of '{Name}' is expected.
I could not understand what is wrong with this schema or my xml.
Solution
all u have to do is add elementFormDefault="qualified" and u will be fine. to understand this behavior, read "Are You Qualified?" section @ http://msdn.microsoft.com/en-us/library/ms950796.aspx
OTHER TIPS
Just add the elementFormDefault="qualified" to the schema attribues.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/testSchema"
elementFormDefault="qualified"
xmlns="http://www.example.org/testSchema">
And your original will work
<?xml version="1.0" encoding="utf-8"?>
<Employee xmlns="http://www.example.org/testSchema">
<Name>
<FirstName>John</FirstName>
<LastName>Smith</LastName>
</Name>
</Employee>
Looks like you're failing to specify how to validate the FirstName
and LastName
elements; give the element specs for those type="xsd:string"
(where xsd
needs to be mapped to the XML Schema Datatypes namespace, of course) and all should be well.
But you are better off not nesting those elements so deep. Put them all at the same level and use ref="Name"
to link them all together instead; it makes your schema much more flexible and usable.