blob: 0237e988f77e72e241af77be37f8982340e875f5 [file] [log] [blame]
<?xml version="1.0" encoding="utf-8"?>
<!--Arbortext, Inc., 1988-2005, v.4002-->
<!DOCTYPE reference PUBLIC "-//OASIS//DTD DITA Reference//EN" "reference.dtd">
<reference id="rnmspc" xml:lang="en-us">
<title>XML namespaces</title>
<titlealts>
<searchtitle>XML namespaces</searchtitle>
</titlealts>
<shortdesc>An XML namespace is a collection of names, identified by a URI
reference, which are used in XML documents as element types and attribute
names.</shortdesc>
<prolog><metadata>
<keywords><indexterm>XML namespaces<indexterm>overview</indexterm></indexterm>
</keywords>
</metadata></prolog>
<refbody>
<section>XML namespaces are defined by a W3C recommendation, dating 14 January
1999, called <xref format="html" href="http://www.w3.org/TR/REC-xml-names/"
scope="external">Namespaces in XML</xref>. XML tag names should be globally
unique, as well as short for performance reasons. In order to resolve this
conflict, the W3C namespace recommendation defines an attribute <b>xmlns</b> which
can amend any XML element. If it is present in an element, it identifies the
namespace for this element.</section>
<section><p>The xmlns attribute has the following syntax:</p><p><codeph>xmlns:<varname>prefix</varname>=namespace</codeph> </p><p>where <codeph>namespace</codeph
> is a unique URI (such as www.ibm.com) and where <codeph><varname>prefix</varname></codeph> represents
the namespace and provides a pointer to it.</p><p>In the following customer
element definition, an accounting namespace is defined in order to be able
to distinguish the element tags from those appearing in customer records created
by other business applications:</p><p><codeblock>&lt;acct:customer xmlns:acct="http://www.my.com/acct-REV10">
&lt;acct:name>Corporation&lt;/acct:name>
&lt;acct:order acct:ref="5566"/>
&lt;acct:status>invoice&lt;/acct:status>
&lt;/acct:customer> </codeblock> </p><p>The <i>namespace definition</i> in
the first line assigns the namespace <i>http://www.my.com/acct-REV10</i> to
the prefix. This prefix is used on the element names such as name in order
to attach them to the namespace. A second application, for example, a fulfillment
system, can assign a different namespace to its customer elements:</p><p><codeblock>&lt;ful:customer xmlns:ful="http://www.your.com/ful">
&lt;ful:name>Corporation&lt;/ful:name>
&lt;ful:order ful:ref="A98756"/>
&lt;ful:status>shipped&lt;/ful:status>
&lt;/ful:customer></codeblock> </p><p>An application processing both data
structures is now able to treat the accounting and the fulfillment data differently.
There is a default namespace. It is set if no local name is assigned in the
namespace definition:</p><p><codeblock>&lt;acct:customer xmlns="http://www.my.com/acct-REV10" xmlns:acct="http://www.my.com/acct-REV10 ">
&lt;name>Corporation&lt;/name>
&lt;order acct:ref="5566"/>
&lt;status>invoice&lt;/status>
&lt;/customer></codeblock></p><p>In this example, all tags in the customer
record are qualified to reside in the namespace <i>http://www.my.com/acct-REV10.</i> No
explicit prefix is needed because the default namespace is used. Note that
the default namespace applies to any attributes definitions.</p></section>
<section><title>XML schemas and namespaces</title><p>In the following XML
schema, the default namespace for the schema is defined as the standard XML
schema namespace <i>http://www.w3.org/2001/XMLSchem</i>a; there is also a
schema specific namespace <i>http://www.ibm.com</i>.</p><p><codeblock>&lt;?xml version="1.0"?>
&lt;schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ibm.com" xmlns:TestSchema="http://www.ibm.com">
&lt;simpleType name="ZipCodeType">
&lt;restriction base="integer">
&lt;minInclusive value="10000"/>
&lt;maxInclusive value="99999"/>
&lt;/restriction>
&lt;/simpleType>
&lt;!--element definitions skipped -->
&lt;/schema> </codeblock></p><p>Assuming that the preceding XML schema is
saved as <filepath>C:\temp\TestSchema.xsd</filepath>, a sample XML file that
validates against this schema is:</p><p><codeblock>&lt;?xml version="1.0"?>
&lt;x:addressList xmlns:x="http://www.ibm.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ibm.com file:///C:/temp/TestSchema.xsd">
xsi:schemaLocation="http://www.ibm.com file:///C:/temp/TestSchema.xsd">
&lt;x:address>
&lt;x:street>x:Vangerowstrasse&lt;/x:street>
&lt;x:zipCode>69115&lt;/x:zipCode>
&lt;x:city>x:Heidelberg&lt;/x:city>
&lt;/x:address>
&lt;x:address>
&lt;x:street>x:Bernal Road&lt;/x:street>
&lt;x:zipCode>90375&lt;/x:zipCode>
&lt;x:city>x:San Jose&lt;/x:city>
&lt;/x:address>
&lt;/x:addressList> </codeblock></p></section>
<section><title>Target namespace</title><p> The target namespace serves to
identify the namespace within which the association between the element and
its name exists. In the case of declarations, this association determines
the namespace of the elements in XML files conforming to the schema. An XML
file importing a schema must reference its target namespace in the schemaLocation
attribute. Any mismatches between the target and the actual namespace of an
element are reported as schema validation errors. In our example, the target
namespace is http://www.ibm.com; it is defined in the XML schema file and
referenced twice in the XML file. Any mismatch between these three occurrences
of the namespace lead to validation errors.</p><p> The following examples
show how target namespaces and namespace prefixes work in XML schemas and
their corresponding XML instance documents.</p></section>
<section><title>Sample 1 - A schema with both a default and target namespace
and unqualified locals</title><p>The XML schema: </p><p><codeblock>&lt;?xml version="1.0"?>
&lt;schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ibm.com" xmlns:x="http://www.ibm.com">
&lt;complexType name="AddressType">
&lt;sequence>
&lt;element name="name" type="string">&lt;/element>
&lt;/sequence>
&lt;/complexType>
&lt;element name="MyAddress" type="x:AddressType">&lt;/element>
&lt;/schema> </codeblock> </p><p>A valid XML instance document created from
this schema looks like this. Local elements and attributes are <i>unqualified</i>.</p><p><codeblock>&lt;?xml version="1.0"?>
&lt;x:MyAddress xmlns:x="http://www.ibm.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ibm.com x.xsd ">
&lt;name>Peter Smith&lt;/name>
&lt;/x:MyAddress> </codeblock></p><p>When local elements (such as the <i>"name"</i> element)
and attributes are unqualified in an XML file, then only the root element
is qualified. So, in this example, the <i>"x"</i> namespace prefix is assigned
to the root element <i>"MyAddress"</i>, associating it with the namespace <i>"http://www.ibm.com",</i> but
the<i>"x"</i> prefix is not assigned to the local element <i>"name"</i>.</p></section>
<section><title>Sample 2 - A schema with both a default and target namespace
and qualified locals</title><p><codeblock>&lt;?xml version="1.0"?>
&lt;schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ibm.com" xmlns:x="http://www.ibm.com" elementFormDefault="qualified">
&lt;complexType name="AddressType">
&lt;sequence>
&lt;element name="name" type="string">&lt;/element>
&lt;/sequence>
&lt;/complexType>
&lt;element name="MyAddress" type="x:AddressType">&lt;/element>
&lt;/schema> </codeblock></p><p>A valid XML instance document created from
this schema looks like this. Local elements and attributes are <i>qualified</i> This
is because the elementFormDefault attribute is set to qualified in the XML
schema.</p><p><codeblock>&lt;?xml version="1.0"?>
&lt;x:MyAddress xmlns:x="http://www.ibm.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.ibm.com x.xsd ">
&lt;x:name>Peter Smith&lt;/x:name>
&lt;/x:MyAddress></codeblock> </p><p>In this example, the <i>"x"</i> namespace
prefix is assigned to both the root element <i>"MyAddress"</i> and the local
element <i>"name"</i>, associating them with the namespace <i>"http://www.ibm.com",</i>.</p></section>
<section><title>Sample 3 - Schema with target Namespace, and explicitly defines
xmlns:xsd</title><p>This XML schema adds this attribute: </p><codeph>xmlns:xsd="http://www.w3.org/2001/XMLSchema </codeph><p>What
this means is that each of the constructs that are defined by the XML schema
language will need to be qualified with the <varname>"xsd"</varname> prefix.
For example, xsd:complexType and xsd:string</p><p>. Note that you can chose
any other prefixes such as <varname>"xs"</varname> or <varname>"foobar"</varname> in
your declaration and usage.</p><p>You can specify this prefix in the XML schema
preferences page. For more information, refer to the related tasks.</p><p>All
user defined types belong to the namespace http://www.ibm.com as defined
by the targetNamespace attribute, and the prefix is <i>"x"</i> as defined
by the xmlns:x attribute.</p><p><codeblock>&lt;?xml version="1.0"?>
&lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ibm.com" xmlns:x="http://www.ibm.com">
&lt;xsd:complexType name="AddressType">
&lt;xsd:sequence>
&lt;xsd:element name="name" type="xsd:string">&lt;/xsd:element>
&lt;/xsd:sequence>
&lt;/xsd:complexType>
&lt;xsd:element name="MyAddress" type="x:AddressType">&lt;/xsd:element>
&lt;/xsd:schema></codeblock> </p><p>A valid XML instance document created
from this schema looks like this. Local elements and attributes are <i>unqualified</i>.
The semantics of qualification is the same as Sample 1.</p><p><codeblock>&lt;?xml version="1.0"?>
&lt;x:MyAddress xmlns:x="http://www.ibm.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.ibm.com x.xsd ">
&lt;name>Peter Smith&lt;/name>
&lt;/x:MyAddress></codeblock></p></section>
<section><title>Sample 4 - Schema with undeclared target Namespace that explicitly
defines xmlns:xsd</title><p>This XML schema has no target namespace for itself.
In this case, it is highly recommended that all XML schema constructs be explicitly
qualified with a prefix such as <i>"xsd"</i>. The definitions and declarations
from this schema such as <i>AddressType</i> are referenced without namespace
qualification since there is no namespace prefix. </p><p><codeblock>&lt;?xml version="1.0"?>
&lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
&lt;xsd:complexType name="AddressType">
&lt;xsd:sequence>
&lt;xsd:element name="name" type="xsd:string">&lt;/xsd:element>
&lt;xsd:element name="name" type="xsd:string">&lt;/xsd:element>
&lt;xsd:element name="name" type="xsd:string">&lt;/xsd:element>
&lt;/xsd:sequence>
&lt;/xsd:complexType>
&lt;xsd:element name="MyAddress" type="AddressType">&lt;/xsd:element>
&lt;/xsd:schema> </codeblock></p><p>A valid XML instance document created
from the schema looks like this. All elements are <i>unqualified</i>.</p><p><codeblock>&lt;?xml version="1.0"?>
&lt;MyAddress xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="x.xsd">
&lt;name>name&lt;/name>
&lt;/MyAddress></codeblock> </p></section>
<section><title>Sample 5 - A schema where the target namespace is the default
namespace</title><p>This is an XML schema where the target namespace is the
default namespace. As well, the namespace has no namespace prefix.</p><p><codeblock>&lt;?xml version="1.0"?>
&lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ibm.com" xmlns="http://www.ibm.com">
&lt;xsd:complexType name="AddressType">
&lt;xsd:sequence>
&lt;xsd:element name="name" type="xsd:string">&lt;/xsd:element>
&lt;/xsd:sequence>
&lt;/xsd:complexType>
&lt;xsd:element name="MyAddress" type="AddressType">&lt;/xsd:element>
&lt;/xsd:schema> </codeblock> </p><p>A valid XML instance document created
from the schema looks like this:</p><p><codeblock>&lt;?xml version="1.0" encoding="UTF-8"?>
&lt;MyAddress xmlns="http://www.ibm.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ibm.com NewXMLSchema.xsd">
&lt;name>name&lt;/name>
&lt;/MyAddress> </codeblock> </p></section>
</refbody>
</reference>