Tag Archives: XML - Page 2

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”)

Validate elements that can be empty with XSD

Say you have an element looking like this:


    Catcher in the rye

and that element is allowed to also be empty. Like this:


    <book/>

How do you validate it? One way is to use the attribute “nillable” like this


  
    
      <xs:element name="book" type="xs:string" nillable="true"/>
        
      
 

This will validate both a “book” element that is empty and one that is not

Validate pattern OR nothing in XSD

Say you have a XML like this:

:
20110506
:

Where <date> is allowed to be empty OR follow pattern ’19|20[0-9]{2}[0-1][0-9][0-3][0-9]’. How do you validate it? The solution is too simple and that is why I didn’t figure it out myself. I googled it and soon found a much smarter guy than me that said to do it like this:


:

    
                   
            <xs:pattern value="(19|20[0-9]{2}[0-1][0-9][0-3][0-9])|"/>
        
    

:

This validates YYYYMMDD type dates and also validate an empty date tag. The solution lies in the last OR operand ‘|’. You see, either the left one (the pattern) is true or the right one (which is empty). Cleaver ah? I certainly thinks soo