Mathias Bader
Mathias Bader

Reputation: 3836

XML-XSD: How to define namespaces correctly?

I am wondering how to define a namespace correctly with XML and XSD. I have the following XML-File:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<application
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:app="ms.xsd"
    xsi:schemaLocation="ms.xsd">
    <app:contactPerson>
        <app:name>Florian</app:name>
        <app:countryCode>FR</app:countryCode>
    </app:contactPerson>
    <app:contactPerson>
        <app:name>Gabi</app:name>
        <app:countryCode>DE,EE,EL,FI,FR,IE,UK</app:countryCode>
    </app:contactPerson>
    <app:contactPerson>
        <app:name>Gert</app:name>
        <app:countryCode>GB</app:countryCode>
    </app:contactPerson>
    <app:origin/>
</application>

And the schema is defined by the file ms.xsd in the same folder:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified"
    version="0.2"
    targetNamespace="ms.xsd"
    xmlns:app="ms.xsd">
    <xs:element name="application" type="app:applicationType"/>
    <xs:complexType name="applicationType">
        <xs:sequence>
            <xs:element name="contactPerson" type="app:contactPersonType" minOccurs="0" maxOccurs="unbounded"/>
            <xs:element name="origin" type="app:originType" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="contactPersonType">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="countryCode" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="originType">
        <xs:sequence minOccurs="0">
            <xs:element name="street" type="xs:string"/>
            <xs:element name="city" type="xs:string"/>
            <xs:element name="country" type="xs:string"/>
            <xs:element name="further_details" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

I already tried a lot of things. The error I get with this (currently) final version is

ERROR: Element 'application': No matching global declaration available for the validation root.

Is this because the xsd-reference in the xml-file is within the application-tag, such that it comes 'too late'? When I add an "envelope"-tag around everything and put the xsd-definition there, I get the message

ERROR: Element 'envelope': No matching global declaration available for the validation root.

So now I'm really confused on how to do this correctly. I am validating the XML against the XSD using XML-Tools in Notepadd++.

Upvotes: 1

Views: 975

Answers (2)

13ren
13ren

Reputation: 12187

Another approach is to add a default namespace:

xmlns="ms.xsd"

This will put the <application> into that namespace. Actually, you could then omit all the other app: prefixes if you wanted to. Though I think @jmcollin92's solution is better - just filling in the gaps.

BTW: Your xsi:schemaLocation should be:

xsi:schemaLocation="ms.xsd ms.xsd"

The first ms.xsd is the namespace (which can be any arbitrary string); the second ms.xsd is the file where the schema can be found (an URI - usually a file or URL) - actually, it can be a list of such pairs. Strictly speaking, your xml tools should have flagged an error.

Upvotes: 1

jmcollin92
jmcollin92

Reputation: 2996

Try app:application ... This should work. No matter that the declaration comes after.

Upvotes: 4

Related Questions