blob: be48e8b245f5f08732dd002908f63fa5503e8643 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="../../com.ibm.help.doc/swg_info_common.css" />
<title>XML namespaces</title>
<script language="JavaScript">
function popup_window( url, id, width, height )
{
popup = window.open( url, id, 'toolbar=no,scrollbars=no,location=no,statusbar=no,menubar=no,resizable=no,width=' + width + ',height=' + height + ',left=,top=' );
popup.focus();
}
</script><script language="JavaScript" src="help/liveHelp.js"></script></head>
<body id="rnmspc"><a name="rnmspc"><!-- --></a>
<h1 class="topictitle1">XML namespaces</h1>
<div><div class="skipspace">XML namespaces are defined by a W3C recommendation, dating 14 January
1999, called <a href="http://www.w3.org/TR/REC-xml-names/">Namespaces
in XML</a>. 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 xmlns which can amend any XML element.
If it is present in an element, it identifies the namespace for this element.</div>
<div class="skipspace"><p>The xmlns attribute has the following syntax:</p>
<p><samp class="codeph"> xmlns: <var class="varname">prefix</var>:namespace</samp> </p>
<p>where <samp class="codeph">namespace</samp> is a unique URI (such as www.ibm.com) and where <samp class="codeph"><var class="varname">prefix</var></samp> 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>
<div class="p"><pre>&lt;acct:customer xmlns:acct="http://www.my.com/acct-REV10"&gt;
&lt;acct:name&gt;Corporation&lt;/acct:name&gt;
&lt;acct:order acct:ref="5566"/&gt;
&lt;acct:status&gt;invoice&lt;/acct:status&gt;
&lt;/acct:customer&gt; </pre>
</div>
<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>
<div class="p"><pre>&lt;ful:customer xmlns:ful="http://www.your.com/ful"&gt;
&lt;ful:name&gt;Corporation&lt;/ful:name&gt;
&lt;ful:order ful:ref="A98756"/&gt;
&lt;ful:status&gt;shipped&lt;/ful:status&gt;
&lt;/ful:customer&gt;</pre>
</div>
<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>
<div class="p"><pre>&lt;acct:customer xmlns="http://www.my.com/acct-REV10" xmlns:acct="http://www.my.com/acct-REV10 "&gt;
&lt;name&gt;Corporation&lt;/name&gt;
&lt;order acct:ref="5566"/&gt;
&lt;status&gt;invoice&lt;/status&gt;
&lt;/customer&gt;</pre>
</div>
<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>
</div>
<div class="skipspace"><h4 class="sectiontitle">XML schemas and namespaces</h4><p>In the XML schema below,
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>
<div class="p"><pre>&lt;?xml version="1.0"?&gt;
&lt;schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ibm.com" xmlns:TestSchema="http://www.ibm.com"&gt;
&lt;simpleType name="ZipCodeType"&gt;
&lt;restriction base="integer"&gt;
&lt;minInclusive value="10000"/&gt;
&lt;maxInclusive value="99999"/&gt;
&lt;/restriction&gt;
&lt;/simpleType&gt;
&lt;!--element definitions skipped --&gt;
&lt;/schema&gt; </pre>
</div>
<p>Assuming that the XML schema defined above
is saved as <span class="filepath">C:\temp\TestSchema.xsd</span>, a sample XML file
that validates against this schema is:</p>
<div class="p"><pre>&lt;?xml version="1.0"?&gt;
&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"&gt;
xsi:schemaLocation="http://www.ibm.com file:///C:/temp/TestSchema.xsd"&gt;
&lt;x:address&gt;
&lt;x:street&gt;x:Vangerowstrasse&lt;/x:street&gt;
&lt;x:zipCode&gt;69115&lt;/x:zipCode&gt;
&lt;x:city&gt;x:Heidelberg&lt;/x:city&gt;
&lt;/x:address&gt;
&lt;x:address&gt;
&lt;x:street&gt;x:Bernal Road&lt;/x:street&gt;
&lt;x:zipCode&gt;90375&lt;/x:zipCode&gt;
&lt;x:city&gt;x:San Jose&lt;/x:city&gt;
&lt;/x:address&gt;
&lt;/x:addressList&gt; </pre>
</div>
</div>
<div class="skipspace"><h4 class="sectiontitle">Target namespace</h4><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 are some
examples of how target namespace and namespace prefixes work in XML schemas
and their corresponding XML instance documents. </p>
</div>
<div class="skipspace"><h4 class="sectiontitle">Sample 1 - A schema with both a default and target namespace
and unqualified locals</h4><p>The XML schema: </p>
<div class="p"><pre>&lt;?xml version="1.0"?&gt;
&lt;schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ibm.com" xmlns:x="http://www.ibm.com"&gt;
&lt;complexType name="AddressType"&gt;
&lt;sequence&gt;
&lt;element name="name" type="string"&gt;&lt;/element&gt;
&lt;/sequence&gt;
&lt;/complexType&gt;
&lt;element name="MyAddress" type="x:AddressType"&gt;&lt;/element&gt;
&lt;/schema&gt; </pre>
</div>
<p>A valid XML instance document created from
this schema looks like this. Local elements and attributes are <i>unqualified</i>.</p>
<div class="p"><pre>&lt;?xml version="1.0"?&gt;
&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 "&gt;
&lt;name&gt;Peter Smith&lt;/name&gt;
&lt;/x:MyAddress&gt; </pre>
</div>
<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>
</div>
<div class="skipspace"><h4 class="sectiontitle">Sample 2 - A schema with both a default and target namespace
and qualified locals</h4><div class="p"><pre>&lt;?xml version="1.0"?&gt;
&lt;schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ibm.com" xmlns:x="http://www.ibm.com" elementFormDefault="qualified"&gt;
&lt;complexType name="AddressType"&gt;
&lt;sequence&gt;
&lt;element name="name" type="string"&gt;&lt;/element&gt;
&lt;/sequence&gt;
&lt;/complexType&gt;
&lt;element name="MyAddress" type="x:AddressType"&gt;&lt;/element&gt;
&lt;/schema&gt; </pre>
</div>
<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>
<div class="p"><pre>&lt;?xml version="1.0"?&gt;
&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 "&gt;
&lt;x:name&gt;Peter Smith&lt;/x:name&gt;
&lt;/x:MyAddress&gt;</pre>
</div>
<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>
</div>
<div class="skipspace"><h4 class="sectiontitle">Sample 3 - Schema with target Namespace, and explicitly defines
xmlns:xsd</h4><p>This XML schema adds this attribute: </p>
<samp class="codeph">xmlns:xsd="http://www.w3.org/2001/XMLSchema </samp><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 <var class="varname">"xsd"</var> prefix.
For example, xsd:complexType and xsd:string</p>
<p>. Note that you can chose
any other prefixes such as <var class="varname">"xs"</var> or <var class="varname">"foobar"</var> 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 below.</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>
<div class="p"><pre>&lt;?xml version="1.0"?&gt;
&lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ibm.com" xmlns:x="http://www.ibm.com"&gt;
&lt;xsd:complexType name="AddressType"&gt;
&lt;xsd:sequence&gt;
     &lt;xsd:element name="name" type="xsd:string"&gt;&lt;/xsd:element&gt;
&lt;/xsd:sequence&gt;
&lt;/xsd:complexType&gt;
&lt;xsd:element name="MyAddress" type="x:AddressType"&gt;&lt;/xsd:element&gt;
&lt;/xsd:schema&gt;</pre>
</div>
<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>
<div class="p"><pre>&lt;?xml version="1.0"?&gt;
&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 "&gt;
&lt;name&gt;Peter Smith&lt;/name&gt;
&lt;/x:MyAddress&gt;</pre>
</div>
</div>
<div class="skipspace"><h4 class="sectiontitle">Sample 4 - Schema with undeclared target Namespace that explicitly
defines xmlns:xsd</h4><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>
<div class="p"><pre>&lt;?xml version="1.0"?&gt;
&lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
&lt;xsd:complexType name="AddressType"&gt;
&lt;xsd:sequence&gt;
&lt;xsd:element name="name" type="xsd:string"&gt;&lt;/xsd:element&gt;
&lt;xsd:element name="name" type="xsd:string"&gt;&lt;/xsd:element&gt;
&lt;xsd:element name="name" type="xsd:string"&gt;&lt;/xsd:element&gt;
&lt;/xsd:sequence&gt;
&lt;/xsd:complexType&gt;
&lt;xsd:element name="MyAddress" type="AddressType"&gt;&lt;/xsd:element&gt;
&lt;/xsd:schema&gt; </pre>
</div>
<p>A valid XML instance document created
from the schema looks like this. All elements are <i>unqualified</i>.</p>
<div class="p"><pre>&lt;?xml version="1.0"?&gt;
&lt;MyAddress xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="x.xsd"&gt;
&lt;name&gt;name&lt;/name&gt;
&lt;/MyAddress&gt;</pre>
</div>
</div>
<div class="skipspace"><h4 class="sectiontitle">Sample 5 - A schema where the target namespace is the default
namespace</h4><p>This is an XML schema where the target namespace is the
default namespace. As well, the namespace has no namespace prefix.</p>
<div class="p"><pre>&lt;?xml version="1.0"?&gt;
&lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ibm.com" xmlns="http://www.ibm.com"&gt;
&lt;xsd:complexType name="AddressType"&gt;
&lt;xsd:sequence&gt;
&lt;xsd:element name="name" type="xsd:string"&gt;&lt;/xsd:element&gt;
&lt;/xsd:sequence&gt;
&lt;/xsd:complexType&gt;
&lt;xsd:element name="MyAddress" type="AddressType"&gt;&lt;/xsd:element&gt;
&lt;/xsd:schema&gt; </pre>
</div>
<p>A valid XML instance document created
from the schema looks like this:</p>
<div class="p"><pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;MyAddress xmlns="http://www.ibm.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ibm.com NewXMLSchema.xsd"&gt;
&lt;name&gt;name&lt;/name&gt;
&lt;/MyAddress&gt; </pre>
</div>
</div>
</div>
<p>
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.
</p>
</body>
</html>