Validate XML with multiple namespaces using xsd

This took some time to figure out. They do not make it easy for us 🙂

I am here going to show the basics of validating an XML with multiple namespaces. In the example I will use two namespaces. XML to validate looks like this:

<?xml version="1.0"?>
<ns:books   xmlns="http://www.my.com"
			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

			xsi:schemaLocation="http://www.my.com/book  book.xsd
                    			http://www.my.com/books books.xsd"
			xmlns:cs="http://www.my.com/book"
			xmlns:ns="http://www.my.com/books">
    
    <cs:book author="Astrid Lindgren"/>
</ns:books>

Here ‘books’ are defined in the ‘ns’ (“http://www.my.com/books”) namespace and ‘book’ is defined in the ‘cs’ (“http://www.my.com/book”) namespace.
We start with defining the ‘ns’ (xmlns:ns=”http://www.my.com/books”) and ‘cs’ (“xmlns:cs=”http://www.my.com/book”) prefix.
We then point out the different xsd files, one for each namespace, with the “schemaLocation” tag. This tag takes the namespace uri and file path as arguments (one on each line is a good rule to keep things tidy)

We then start defining the xsd for the ‘books’ tag (books.xsd):

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.my.com/books" 
            xmlns:cs="http://www.my.com/book"
            elementFormDefault="qualified">
                
<xs:import namespace="http://www.my.com/book" 
       schemaLocation="book.xsd"/>
       
    <xs:element name="books">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="cs:book"/>      
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Here we set the “targetNamespace” to our books uri. We also have to define ‘xmlns:cs=”http://www.my.com/book”‘ due to the book tag inside our books element (cs:book). The schema also needs to know where the book definition is so we import the book xsd with the import-tag
After the import we use the ref attribute to reference the book element. This element is defined in the book.xsd file:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.my.com/book" 
            elementFormDefault="qualified">

	<xs:element name="book">
    	<xs:complexType>
	      	<xs:attribute name="author" type="xs:string"/>
    	</xs:complexType> 
	</xs:element>      
</xs:schema>

The only thing to think about here is to set the “targetNamespace” to the book uri (“http://www.my.com/book”)

  1. NotMadAtAll

    Great stuff!

    I was trying to make it since yesterday but trying to use
    <xs:element name="ab" type="yz" …

    instead of
    <xs:element ref="namespace:name" …

    Saved me at least this day of head ache…

  2. Thanks for the short and precise description, helped me a lot :-).

Reply to NotMadAtAll ¬
Cancel reply


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">

This site uses Akismet to reduce spam. Learn how your comment data is processed.