XML Namespaces

From
Revision as of 23:32, 24 January 2005 by 217.230.233.188 (talk)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction

The purpose of the XML Schema language consists in a XML document’s validation. Thus, you can create application specific constraints and determine the hierarchy and relationship of your elements by using XML Schema.

In order to validate a document against different XML Schemata, the concept of namespaces has been introduced. Basically, prefixes are assigned to URIs, so that parsers are able to identify and separate elements in the document. The attribute xmlns indicates, that a namespace declaration follows:

  Xmlns:Prefix=URI


Target namespace

To identify a XML schema out of XML documents, it has to be named with a specific URI in the schema itself.

<? xml version = '1.0' encoding='UTF-8' ?>
 <schema xmlns="http://www.w3.org/2001/XMLSchema">
   targetNamespace="http://www.taxi.com/TaxiReservation ">
 <complexType name="ReservationType">
...


Default namespace

The declaration of a default namespace allows to avoid prefixing each element. Once declared, it applies to the element in which it is specified as well as all subelements.

<? xml version = '1.0' encoding='UTF-8' ?>
 <reservation xmlns="http://www.taxi.com/TaxiReservation">
  <customer>
    <last-name>Turner</last-name>
    <first-name>Tine</first-name>
  </customer>
 </reservation>

schemaLocation

Since a namespace prefix is assigned to an URI, which does not indicate the physical location of the Schema, a validation of the xml document can not take place yet. You need to specify the location of the schema. Therefore, the attribute schemaLocation is used. It defines the specific identifier (URI) as well as the physical location (URL). Due to the fact, that schemaLocation is not part of the XML language, you need to assign a namespace to this attribute as well.

<? xml version = '1.0' encoding='UTF-8' ?>
 <res:reservation xmlns="http://www.taxi.com/TaxiReservation">
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-Instance"
    xsi:schemaLocation="http://www.taxi.com/TaxiReservation
                        http://www.taxi.com/schemas/taxiReservation.xsd">
     <customer>
     <last-name>Turner</last-name>
...


Namespaces in XML Schemata

The concept of XML namespace declaration can be used in XML Schemata as well. For instance, the XML schema specification, which defines elements such as <complexType> or the build-in data types, needs to be defined in each XML Schema.

<? xml version = '1.0' encoding='UTF-8' ?>
 <schema
 <xmlns="http://www.w3.org/2001/XMLSchema">
 <targetNamespace="http://www.taxi.com/Address"
 <xmlns:addr="http://www.taxi.com/Address>
   <element name="address" type="addr:AddressType"/>
   <complexType name="AddressTyp"“>
   <sequence>
     <element name="street" type="string"/>
     <element name="city" type="string"/>
...