blob: 315331d1a8d5548c7154e2a3e32996cd92cfd9df [file] [log] [blame]
kprice159fa582006-05-08 21:30:45 +00001<?xml version="1.0" encoding="utf-8"?>
kpricede20a692008-04-24 00:27:47 +00002<!--Arbortext, Inc., 1988-2006, v.4002-->
3<!DOCTYPE reference PUBLIC "-//OASIS//DTD DITA Reference//EN"
4 "reference.dtd">
kprice159fa582006-05-08 21:30:45 +00005<reference id="rnmspc" xml:lang="en-us">
6<title>XML namespaces</title>
8<searchtitle>XML namespaces</searchtitle>
10<shortdesc>An XML namespace is a collection of names, identified by a URI
11reference, which are used in XML documents as element types and attribute
14<keywords><indexterm>XML namespaces<indexterm>overview</indexterm></indexterm>
kpricede20a692008-04-24 00:27:47 +000018<section>XML namespaces are defined by a W3C recommendation, dating 16 August
192006, called <xref format="html" href=""
kprice159fa582006-05-08 21:30:45 +000020scope="external">Namespaces in XML</xref>. XML tag names should be globally
21unique, as well as short for performance reasons. In order to resolve this
22conflict, the W3C namespace recommendation defines an attribute <b>xmlns</b> which
23can amend any XML element. If it is present in an element, it identifies the
24namespace for this element.</section>
25<section><p>The xmlns attribute has the following syntax:</p><p><codeph>xmlns:<varname>prefix</varname>=namespace</codeph> </p><p>where <codeph>namespace</codeph
26> is a unique URI (such as and where <codeph><varname>prefix</varname></codeph> represents
27the namespace and provides a pointer to it.</p><p>In the following customer
28element definition, an accounting namespace is defined in order to be able
29to distinguish the element tags from those appearing in customer records created
30by other business applications:</p><p><codeblock>&lt;acct:customer xmlns:acct="">
31 &lt;acct:name>Corporation&lt;/acct:name>
32 &lt;acct:order acct:ref="5566"/>
33 &lt;acct:status>invoice&lt;/acct:status>
kpricede20a692008-04-24 00:27:47 +000034&lt;/acct:customer> </codeblock> </p><p>The namespace definition in the first
35line assigns the namespace to the prefix. This
36prefix is used on the element names such as name in order to attach them to
37the namespace. A second application, for example, a fulfillment system, can
38assign a different namespace to its customer elements:</p><p><codeblock>&lt;ful:customer xmlns:ful="">
kprice159fa582006-05-08 21:30:45 +000039 &lt;ful:name>Corporation&lt;/ful:name>
40 &lt;ful:order ful:ref="A98756"/>
41 &lt;ful:status>shipped&lt;/ful:status>
42 &lt;/ful:customer></codeblock> </p><p>An application processing both data
43structures is now able to treat the accounting and the fulfillment data differently.
44There is a default namespace. It is set if no local name is assigned in the
45namespace definition:</p><p><codeblock>&lt;acct:customer xmlns="" xmlns:acct=" ">
47&lt;order acct:ref="5566"/>
49&lt;/customer></codeblock></p><p>In this example, all tags in the customer
kpricede20a692008-04-24 00:27:47 +000050record are qualified to reside in the namespace
51No explicit prefix is needed because the default namespace is used. Note that
kprice159fa582006-05-08 21:30:45 +000052the default namespace applies to any attributes definitions.</p></section>
53<section><title>XML schemas and namespaces</title><p>In the following XML
54schema, the default namespace for the schema is defined as the standard XML
kpricede20a692008-04-24 00:27:47 +000055schema namespace; there is also a schema
56specific namespace</p><p><codeblock>&lt;?xml version="1.0"?>
kprice159fa582006-05-08 21:30:45 +000057&lt;schema xmlns="" targetNamespace="" xmlns:TestSchema="">
58 &lt;simpleType name="ZipCodeType">
59 &lt;restriction base="integer">
60 &lt;minInclusive value="10000"/>
61 &lt;maxInclusive value="99999"/>
63 &lt;/simpleType>
64 &lt;!--element definitions skipped -->
65&lt;/schema> </codeblock></p><p>Assuming that the preceding XML schema is
66saved as <filepath>C:\temp\TestSchema.xsd</filepath>, a sample XML file that
67validates against this schema is:</p><p><codeblock>&lt;?xml version="1.0"?>
68&lt;x:addressList xmlns:x="" xmlns:xsi="" xsi:schemaLocation=" file:///C:/temp/TestSchema.xsd">
69 xsi:schemaLocation=" file:///C:/temp/TestSchema.xsd">
71 &lt;x:street>x:Vangerowstrasse&lt;/x:street>
72 &lt;x:zipCode>69115&lt;/x:zipCode>
73 &lt;x:city>x:Heidelberg&lt;/x:city>
74 &lt;/x:address>
75 &lt;x:address>
76&lt;x:street>x:Bernal Road&lt;/x:street>
78 &lt;x:city>x:San Jose&lt;/x:city>
79 &lt;/x:address>
80&lt;/x:addressList> </codeblock></p></section>
81<section><title>Target namespace</title><p> The target namespace serves to
82identify the namespace within which the association between the element and
83its name exists. In the case of declarations, this association determines
84the namespace of the elements in XML files conforming to the schema. An XML
85file importing a schema must reference its target namespace in the schemaLocation
86attribute. Any mismatches between the target and the actual namespace of an
87element are reported as schema validation errors. In our example, the target
88namespace is; it is defined in the XML schema file and
89referenced twice in the XML file. Any mismatch between these three occurrences
90of the namespace lead to validation errors.</p><p> The following examples
91show how target namespaces and namespace prefixes work in XML schemas and
92their corresponding XML instance documents.</p></section>
93<section><title>Sample 1 - A schema with both a default and target namespace
94and unqualified locals</title><p>The XML schema: </p><p><codeblock>&lt;?xml version="1.0"?>
95&lt;schema xmlns="" targetNamespace="" xmlns:x="">
96&lt;complexType name="AddressType">
98&lt;element name="name" type="string">&lt;/element>
101&lt;element name="MyAddress" type="x:AddressType">&lt;/element>
102&lt;/schema> </codeblock> </p><p>A valid XML instance document created from
kpricede20a692008-04-24 00:27:47 +0000103this schema looks like this. Local elements and attributes are unqualified.</p><p><codeblock>&lt;?xml version="1.0"?>
kprice159fa582006-05-08 21:30:45 +0000104&lt;x:MyAddress xmlns:x="" xmlns:xsi="" xsi:schemaLocation=" x.xsd ">
105&lt;name>Peter Smith&lt;/name>
kpricede20a692008-04-24 00:27:47 +0000106&lt;/x:MyAddress> </codeblock></p><p>When local elements (such as the "name"
107element) and attributes are unqualified in an XML file, then only the root
108element is qualified. So, in this example, the "x" namespace prefix is assigned
109to the root element "MyAddress", associating it with the namespace "",
110but the"x" prefix is not assigned to the local element "name".</p></section>
kprice159fa582006-05-08 21:30:45 +0000111<section><title>Sample 2 - A schema with both a default and target namespace
112and qualified locals</title><p><codeblock>&lt;?xml version="1.0"?>
113&lt;schema xmlns="" targetNamespace="" xmlns:x="" elementFormDefault="qualified">
114&lt;complexType name="AddressType">
116&lt;element name="name" type="string">&lt;/element>
119&lt;element name="MyAddress" type="x:AddressType">&lt;/element>
120 &lt;/schema> </codeblock></p><p>A valid XML instance document created from
kpricede20a692008-04-24 00:27:47 +0000121this schema looks like this. Local elements and attributes are qualified This
kprice159fa582006-05-08 21:30:45 +0000122is because the elementFormDefault attribute is set to qualified in the XML
123schema.</p><p><codeblock>&lt;?xml version="1.0"?>
124 &lt;x:MyAddress xmlns:x="" xmlns:xsi=""
125 xsi:schemaLocation=" x.xsd ">
126&lt;x:name>Peter Smith&lt;/x:name>
kpricede20a692008-04-24 00:27:47 +0000127 &lt;/x:MyAddress></codeblock> </p><p>In this example, the "x" namespace prefix
128is assigned to both the root element "MyAddress" and the local element "name",
129associating them with the namespace "",.</p></section>
kprice159fa582006-05-08 21:30:45 +0000130<section><title>Sample 3 - Schema with target Namespace, and explicitly defines
131xmlns:xsd</title><p>This XML schema adds this attribute: </p><codeph>xmlns:xsd=" </codeph><p>What
132this means is that each of the constructs that are defined by the XML schema
kpricede20a692008-04-24 00:27:47 +0000133language will need to be qualified with the "xsd" prefix. For example, xsd:complexType
134and xsd:string</p><p>. Note that you can chose any other prefixes such as
135"xs" or "foobar" in your declaration and usage.</p><p>You can specify this
136prefix in the XML schema preferences page. For more information, refer to
137the related tasks.</p><p>All user defined types belong to the namespace
138as defined by the targetNamespace attribute, and the prefix is "x" as defined
kprice159fa582006-05-08 21:30:45 +0000139by the xmlns:x attribute.</p><p><codeblock>&lt;?xml version="1.0"?>
140&lt;xsd:schema xmlns:xsd="" targetNamespace="" xmlns:x="">
141&lt;xsd:complexType name="AddressType">
143 &lt;xsd:element name="name" type="xsd:string">&lt;/xsd:element>
145 &lt;/xsd:complexType>
146 &lt;xsd:element name="MyAddress" type="x:AddressType">&lt;/xsd:element>
147&lt;/xsd:schema></codeblock> </p><p>A valid XML instance document created
kpricede20a692008-04-24 00:27:47 +0000148from this schema looks like this. Local elements and attributes are unqualified.
kprice159fa582006-05-08 21:30:45 +0000149The semantics of qualification is the same as Sample 1.</p><p><codeblock>&lt;?xml version="1.0"?>
150 &lt;x:MyAddress xmlns:x="" xmlns:xsi=""xsi:schemaLocation=" x.xsd ">
151&lt;name>Peter Smith&lt;/name>
152 &lt;/x:MyAddress></codeblock></p></section>
153<section><title>Sample 4 - Schema with undeclared target Namespace that explicitly
154defines xmlns:xsd</title><p>This XML schema has no target namespace for itself.
155In this case, it is highly recommended that all XML schema constructs be explicitly
kpricede20a692008-04-24 00:27:47 +0000156qualified with a prefix such as "xsd". The definitions and declarations from
157this schema such as AddressType are referenced without namespace qualification
158since there is no namespace prefix. </p><p><codeblock>&lt;?xml version="1.0"?>
kprice159fa582006-05-08 21:30:45 +0000159&lt;xsd:schema xmlns:xsd="">
160&lt;xsd:complexType name="AddressType">
162&lt;xsd:element name="name" type="xsd:string">&lt;/xsd:element>
163&lt;xsd:element name="name" type="xsd:string">&lt;/xsd:element>
164&lt;xsd:element name="name" type="xsd:string">&lt;/xsd:element>
167&lt;xsd:element name="MyAddress" type="AddressType">&lt;/xsd:element>
168&lt;/xsd:schema> </codeblock></p><p>A valid XML instance document created
kpricede20a692008-04-24 00:27:47 +0000169from the schema looks like this. All elements are unqualified.</p><p><codeblock>&lt;?xml version="1.0"?>
kprice159fa582006-05-08 21:30:45 +0000170&lt;MyAddress xmlns:xsi="" xsi:noNamespaceSchemaLocation="x.xsd">
172&lt;/MyAddress></codeblock> </p></section>
173<section><title>Sample 5 - A schema where the target namespace is the default
174namespace</title><p>This is an XML schema where the target namespace is the
175default namespace. As well, the namespace has no namespace prefix.</p><p><codeblock>&lt;?xml version="1.0"?>
176 &lt;xsd:schema xmlns:xsd="" targetNamespace="" xmlns="">
177&lt;xsd:complexType name="AddressType">
179&lt;xsd:element name="name" type="xsd:string">&lt;/xsd:element>
182 &lt;xsd:element name="MyAddress" type="AddressType">&lt;/xsd:element>
183 &lt;/xsd:schema> </codeblock> </p><p>A valid XML instance document created
184from the schema looks like this:</p><p><codeblock>&lt;?xml version="1.0" encoding="UTF-8"?>
185&lt;MyAddress xmlns="" xmlns:xsi="" xsi:schemaLocation=" NewXMLSchema.xsd">
187 &lt;/MyAddress> </codeblock> </p></section>
kpricede20a692008-04-24 00:27:47 +0000189</reference><?Pub Caret?>
190<?Pub *0000012002?>