2

I am tyring to validate a xml against a xsd. The following is the xsd

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns="http://www.w3schools.com" targetNamespace="http://www.xxxxxxxxxxxxx/xxxxxxxx" xmlns:cl="http://www.xxxxxx/contactlist" xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

    <xsd:complexType name="contactNumberType">
        <xsd:all>
            <xsd:element name="type" type="xsd:string" minOccurs="1" maxOccurs="1"/>
            <xsd:element name="number" type="xsd:integer" minOccurs="1" maxOccurs="1"/>
        </xsd:all>
    </xsd:complexType>

    <xsd:complexType name="contactNumbersType">
        <xsd:sequence>
            <xsd:element name="contact_number" type="contactNumberType" minOccurs="1" maxOccurs="2"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="contactType">
        <xsd:all>
            <xsd:element name="name" type="xsd:string" minOccurs="1" maxOccurs="1"/>
            <xsd:element name="company" type="xsd:string" minOccurs="0" maxOccurs="1"/>
            <xsd:element name="jobtitle" type="xsd:string" minOccurs="0" maxOccurs="1"/>
            <xsd:element name="emailadress" type="xsd:string" minOccurs="1" maxOccurs="1"/>
            <xsd:element name="addresses" type="addressesType" minOccurs="0" maxOccurs="1"/>
            <xsd:element name="contact_numbers" type="contactNumbersType" minOccurs="1" maxOccurs="1"/>
        </xsd:all>
    </xsd:complexType>

    <xsd:complexType name="addressType">
        <xsd:all>
            <xsd:element name="type" type="xsd:string" minOccurs="1" maxOccurs="1"/>
            <xsd:element name="street_address1" type="xsd:string" minOccurs="1" maxOccurs="1"/>
            <xsd:element name="street_address2" type="xsd:string" minOccurs="0" maxOccurs="1"/>
            <xsd:element name="suburb" type="xsd:string" minOccurs="1" maxOccurs="1"/>
            <xsd:element name="postcode" type="xsd:integer" minOccurs="1" maxOccurs="1"/>
            <xsd:element name="state" type="xsd:string" minOccurs="1" maxOccurs="1"/>
        </xsd:all>
    </xsd:complexType>

    <xsd:complexType name="contacts">
        <xsd:sequence>
            <xsd:element name="contact" type="contactType" minOccurs="1" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="addressesType">
        <xsd:sequence>
            <xsd:element name="address" type="addressType" minOccurs="1" maxOccurs="2"/>
        </xsd:sequence>
    </xsd:complexType>

</xsd:schema>

I am getting the following exception. Please help me understand what is that I am missing

Exception: src-resolve.4.2: Error resolving component 'contactNumberType'.

1 Answer 1

6

Given the authoring approach, you have to make sure that the default namespace (xmlns attribute's value) matches the targetNamespace attribute's value.

When you reference a type, attribute, attributeGroup, element or group by name, that name is a qualified name. If the name you reference is without a prefix, then it is assumed to be in the default namespace, if specified, or no namespace at all. Having a default namespace of http://www.w3schools.com, the processor is looking for {http://www.w3schools.com}contactNumberType; your XSD defines a {http://www.xxxxxxxxxxxxx/xxxxxxxx}contactNumberType, which obviously don't match. Fixing the default namespace, fixes your reference.

xmlns="http://www.xxxxxxxxxxxxx/xxxxxxxx" targetNamespace="http://www.xxxxxxxxxxxxx/xxxxxxxx"

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help. I have new problem. My xsd is <xsd:schema targetNamespace="mycompany.com.au/contactlist" xmlns="mycompany.com.au/contactlist" xmlns:xsd="w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified"> and xml is <contacts xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns="mycompany.com.au/contactlist" xsi:schemaLocation="mycompany.com.au/contactlist XML_Assignment1.xsd">. Now the error is Exception: cvc-elt.1: Cannot find the declaration of element 'contacts'
I found and fixed the problem as well my xsd is missing the xsd:element specification. All the elements are mentiioned as complexType but no actual element. Thanks for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.