Using the xmlns:xml namespace when validating with xsd

The xml namespace is the default namespace but sometimes you need to define it when working with xsd. The secret here is that the xml namespace only accepts one particular uri (“http://www.w3.org/XML/1998/namespace”). Here is an example of an xsd validation using the xml namespace:
XML to validate

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

		xsi:schemaLocation="http://www.w3.org/XML/1998/namespace xml.xsd
        http://www.my.com my.xsd"

		xmlns:xml="http://www.w3.org/XML/1998/namespace">
    
    <book xml:author="Astrid Lindgren"/>
</books>

To validate the xml:author xml we use the schema definition below (xml.xsd):

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.w3.org/XML/1998/namespace" 
            elementFormDefault="qualified">
                
       <xs:attribute name="author" type="xs:string"/>

</xs:schema>

Notice the “targetNamespace”

Leave a Comment


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>