xsd validation error

Having trouble installing Oxygen? Got a bug to report? Post it all here.
whitney

xsd validation error

Post by whitney »

Hi I am trying to validate this document:

<?xml version="1.0" encoding="UTF-8"?>
<contact xmlns="http://www.hunterfam.com/xml/contact"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hunterfam.com/xml/contact
file:/D:/Development/projects/xmlschema/contact.xsd">
<name>Bob Smith</name>
<phone>(123) 456-7890</phone>
</contact>

with this schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.hunterfam.com/xml/contact"
xmlns="http://www.hunterfam.com/xml/contact"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="contact">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="phone" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

I am getting this error:

- [ contact.xml] E cvc-complex-type.2.4.a: Invalid content starting with element 'name'. One of '{"":name}' is expected. (4:1)

Can you tell me why?

Thanks,
Whitney Hunter
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Dear Whitney,

There is an attribute elementFormDefault that can be used to specify what namespace the local defined elements belong to. By default its value is unqualified meaning that local elements (like name and phone in your example) belong to no namespace. If you specify its value as qualified then the local defined elements will belong to the schema target namespace.
Your schema does not specify this attribute thus the name and phone should have no namespace. In your instance document you define that elements with no prefix belong to http://www.hunterfam.com/xml/contact using the xmlns attribute therefore the name and phone elements are from this namespace.
You have more possibilities to solve this. One is to specify a prefix for the http://www.hunterfam.com/xml/contact namespace thus leaving no prefix for no namespace elements:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<c:contact xmlns:c="http://www.hunterfam.com/xml/contact"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.hunterfam.com/xml/contact contact.xsd">
<name>Bob Smith</name>
<phone>(123) 456-7890</phone>
</c:contact>
Another thing is to reset the default namespace to no namespace for the name and phone elements:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<contact xmlns="http://www.hunterfam.com/xml/contact"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.hunterfam.com/xml/contact contact.xsd">
<name xmlns="">Bob Smith</name>
<phone xmlns="">(123) 456-7890</phone>
</contact>
But you may want to have the name and phone elements belonging to the http://www.hunterfam.com/xml/contact namespace. In this case you should modify the schema to specify the elementFormDefault attribute and set its value to qualified:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.hunterfam.com/xml/contact"
xmlns="http://www.hunterfam.com/xml/contact" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="contact">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="phone" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
In this case the instance document will be validated without any change.

Best Regards,
George
Post Reply