Please refer to theerrata for this document, which may include some normative corrections.
See alsotranslations.
This document is also available in these non-normative formats:XML.
Copyright © 2004 W3C® (MIT,ERCIM,Keio), All Rights Reserved. W3Cliability,trademark,document use andsoftware licensing rules apply.
XML namespaces provide a simple method for qualifying element and attributenames used in Extensible Markup Language documents by associating themwith namespaces identified byIRI references.
This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in theW3C technical reports index at http://www.w3.org/TR/.
This document is aRecommendation of the W3C. It has been reviewed by W3C Members and other interested parties, and hasbeen endorsed by the Director as a W3C Recommendation. It is a stable document and may be used as reference material or cited as a normative reference from another document. W3C's role in making theRecommendation is to draw attention to the specification and to promote its widespread deployment. This enhances the functionality and interoperability of the Web.
This documentis a product of theW3C XML Activity.The English version of this specification is the only normative version. However,for translations of this document, seehttp://www.w3.org/2003/03/Translations/byTechnology?technology=xml-names11.
Documentation of intellectual property possibly relevant to this recommendationmay be found at the Working Group's publicIPR disclosure page.
Known implementations are documented in theNamespaces 1.1 implementation report. A test suite is also available via theXML Test Suitepage.
Please report errors in this document toxml-names-editor@w3.org; publicarchives are available. The errata list for this document is availableathttp://www.w3.org/XML/2004/xml-names11-errata.
1Motivation and Summary
1.1A Note on Notation and Usage
2XML Namespaces
2.1Basic Concepts
2.2Use of IRIs as Namespace Names
2.3Comparing IRI References
3Declaring Namespaces
4Qualified Names
5Using Qualified Names
6Applying Namespaces to Elements and Attributes
6.1Namespace Scoping
6.2Namespace Defaulting
6.3Uniqueness of Attributes
7Conformance of Documents
8Conformance of Processors
9Internationalized Resource Identifiers (IRIs)
ANormative References
BOther references (Non-Normative)
CThe Internal Structure of XML Namespaces (Non-Normative)
DChanges since version 1.0 (Non-Normative)
EAcknowledgements (Non-Normative)
We envision applications of Extensible Markup Language (XML) wherea single XML document maycontain elements and attributes(here referred to as a "markup vocabulary")that are defined for and used by multiple software modules.One motivation for this is modularity: if such a markup vocabulary existswhich is well-understood and for which there is useful software available, it is better to re-use this markup rather than re-invent it.
Such documents, containing multiple markup vocabularies,pose problems of recognition and collision. Software modules need tobe able to recognize the elements and attributes which they are designedto process, even in the face of "collisions" occurring when markup intended for some other softwarepackage uses the same elementnameor attribute name.
These considerations require that document constructsshould have names constructed so as to avoid clashesbetween names from different markup vocabularies.This specification describes a mechanism,XML namespaces, which accomplishes thisby assigningexpanded namesto elements and attributes.
WhereEMPHASIZED, the key wordsMUST,MUST NOT,REQUIRED,SHOULD,SHOULD NOT,MAYin this document are to be interpreted as described in[Keywords].
Note that many of thenonterminals in the productions in this specification are defined not here but inthe XML specification[XML].When nonterminals defined here have the same names as nonterminalsdefined in the XML specification, the productions herein all cases match a subset of the strings matched by the corresponding ones there.
In this document's productions, theNSC
is a "Namespace Constraint", one of the rules that documents conforming to this specificationMUSTfollow.
[Definition: AnXML namespace is identified byanIRI reference;element and attribute namesmay be placed in an XML namespace using the mechanisms describedin this specification.]
[Definition: Anexpanded nameis a pair consisting of anamespace nameand alocal name.][Definition: For a nameN in a namespace identifiedby an IRII, thenamespace nameisI. For a nameN that is not in a namespace, thenamespace namehas no value.][Definition: In either case thelocal nameisN.]It is this combination of the universally managed IRI namespacewith the vocabulary's local names that is effective in avoidingname clashes.
IRI references can contain characters not allowed in names, and are ofteninconveniently long, so expanded names are not used directly to nameelements and attributes in XML documents. Insteadqualified namesare used.[Definition: Aqualified nameis a name subject to namespace interpretation.]In documents conforming to this specification,element and attribute names appear as qualified names.Syntactically, they are eitherprefixed names orunprefixed names.An attribute-based declaration syntax is provided to bind prefixes tonamespace names and to bind a default namespace that applies tounprefixed element names;these declarations are scoped by the elements on which they appear so thatdifferent bindings may apply in different parts of a document.Processors conforming to this specificationMUSTrecognize and act on these declarations and prefixes.
The empty string, though it is a legal IRIreference, cannot be used as a namespace name.
The use of relative IRI references,including same-document references, in namespace declarations isdeprecated.
Note:
This deprecation of relative URI references was decided on by aW3C XML Plenary Ballot[Relative URI deprecation]. It also declares that"later specifications such as DOM, XPath, etc. will define nointerpretation for them".
IRI references identifying namespaces are compared when determiningwhether a name belongs to a given namespace, and whether two namesbelong to the same namespace.[Definition: The two IRIs are treated as strings, and they areidenticalif and only if the strings are identical, that is, if theyare the same sequence of characters. ]The comparison is case-sensitive, and no %-escaping is done or undone.
A consequence of this is that IRI references which are not identicalin this sense may resolve to the same resource. Examples includeIRI references which differ only in case or %-escaping, or which arein external entities which have different base URIs (but note thatrelative IRIs are deprecated as namespace names).
In a namespace declaration, the IRI reference is thenormalized valueof the attribute, so replacement of XML character and entity referenceshas already been done before any comparison.
Examples:
The IRI references below are all different for the purposes of identifyingnamespaces, since they differ in case:
http://www.example.org/wine
http://www.Example.org/wine
http://www.example.org/Wine
The IRI references below are also all different for the purposes of identifyingnamespaces:
http://www.example.org/rosé
http://www.example.org/ros%c3%a9
http://www.example.org/ros%c3%A9
http://www.example.org/ros%C3%a9
http://www.example.org/ros%C3%A9
As are these:
http://www.example.org/~wilbur
http://www.example.org/%7ewilbur
http://www.example.org/%7Ewilbur
If the entityeacute has been defined to beé,the start tags below all contain namespace declarations bindingthe prefixpto the same IRI reference,http://example.org/rosé
.
<p:foo xmlns:p="http://example.org/rosé">
<p:foo xmlns:p="http://example.org/rosé">
<p:foo xmlns:p="http://example.org/rosé">
<p:foo xmlns:p="http://example.org/rosé">
<p:foo xmlns:p="http://example.org/rosé">
Because of the risk of confusion between IRIs that would be equivalentif dereferenced, the use of %-escaped characters in namespace names isstrongly discouraged.
[Definition: A namespace(or more precisely, a namespace binding)isdeclared using a family of reserved attributes.Such an attribute's name must eitherbexmlns orbeginxmlns:.These attributes, like any other XML attributes, may be provideddirectly or bydefault.]
[1] | NSAttName | ::= | PrefixedAttName | |
|DefaultAttName | ||||
[2] | PrefixedAttName | ::= | 'xmlns:'NCName | [NSC: Reserved Prefixes and Namespace Names] |
[3] | DefaultAttName | ::= | 'xmlns' | |
[4] | NCName | ::= | NCNameStartCharNCNameChar* | /* An XMLName, minus the ":" */ |
[5] | NCNameChar | ::= | NameChar- ':' | |
[5a] | NCNameStartChar | ::= | NameStartChar- ':' |
The attribute'snormalized valueMUSTbe either an IRI reference — thenamespace nameidentifying the namespace —or an empty string.The namespace name, to serve itsintended purpose,SHOULDhave the characteristics of uniqueness andpersistence.It is not a goal that it be directly usable for retrieval of a schema (ifany exists). Uniform Resource Names[RFC2141] is an example of a syntax thatis designed with these goals in mind.However, it should be noted that ordinary URLs can be managed in such a way asto achieve these same goals.
[Definition: If theattribute name matchesPrefixedAttName,then theNCName gives thenamespace prefix,used to associate element and attribute names with thenamespace name in the attribute valuein the scope of the element to which the declarationis attached.]
[Definition: If the attribute name matchesDefaultAttName,then thenamespace name in theattribute value isthat of thedefault namespacein the scope of the element to which the declarationis attached.]Default namespaces and overriding of declarations are discussed in6 Applying Namespaces to Elements and Attributes.
An example namespace declaration, which associates the namespace prefixedi with the namespace namehttp://ecommerce.example.org/schema
:
<x xmlns:edi='http://ecommerce.example.org/schema'> <!-- the "edi" prefix is bound to http://ecommerce.example.org/schema for the "x" element and contents --></x>
Namespace constraint: Reserved Prefixes and Namespace Names
The prefixxml is by definition bound to the namespace namehttp://www.w3.org/XML/1998/namespace
. ItMAY,but need not, bedeclared, andMUST NOTbe undeclared or bound to any other namespace name. Other prefixesMUST NOTbe bound to this namespace name.
The prefixxmlns is used only to declare namespace bindings and is bydefinition bound to the namespace namehttp://www.w3.org/2000/xmlns/
. ItMUST NOTbe declared or undeclared. Other prefixesMUST NOTbe bound to this namespace name.
All other prefixes beginning with the three-letter sequence x, m, l,in any case combination, are reserved. This means that:
usersSHOULD NOT use them except as defined by later specifications
processorsMUST NOTtreat them as fatal errors.
Though they are not themselves reserved, it is inadvisable to useprefixed names whose LocalPart begins with the letters x, m, l, in anycase combination, asthese names would be reserved if used without a prefix.
In XMLdocuments conforming to this specification, somenames (constructs corresponding to the nonterminalName)MUSTbe given asqualified names,defined as follows:
[6] | QName | ::= | PrefixedName |
|UnprefixedName | |||
[6a] | PrefixedName | ::= | Prefix ':'LocalPart |
[6b] | UnprefixedName | ::= | LocalPart |
[7] | Prefix | ::= | NCName |
[8] | LocalPart | ::= | NCName |
ThePrefix provides thenamespace prefixpart of the qualified name, andMUSTbe associated with a namespaceIRIreference in anamespace declaration.[Definition: TheLocalPart provides thelocal part of the qualified name.]
Note that the prefix functionsonly as a placeholder for anamespace name.ApplicationsSHOULDuse the namespace name, not the prefix, in constructingnames whose scope extends beyond the containing document.
In XML documents conforming to this specification,elementnames are given asqualified names, asfollows:
[9] | STag | ::= | '<'QName (SAttribute)*S? '>' | [NSC: Prefix Declared] |
[10] | ETag | ::= | '</'QNameS? '>' | [NSC: Prefix Declared] |
[11] | EmptyElemTag | ::= | '<'QName (SAttribute)*S? '/>' | [NSC: Prefix Declared] |
An example of a qualified name serving as an element name:
<!-- the 'price' element's namespace is http://ecommerce.example.org/schema --> <edi:price xmlns:edi='http://ecommerce.example.org/schema' units='Euro'>32.18</edi:price>
Attributes are eithernamespacedeclarationsor their names are given asqualified names:
[12] | Attribute | ::= | NSAttNameEqAttValue | |
|QNameEqAttValue | [NSC: Prefix Declared] |
An example of a qualified name serving as an attribute name:
<x xmlns:edi='http://ecommerce.example.org/schema'> <!-- the 'taxClass' attribute's namespace is http://ecommerce.example.org/schema --> <lineItem edi:taxClass="exempt">Baby food</lineItem></x>
Namespace constraint: Prefix Declared
The namespace prefix, unless it isxml
orxmlns
,MUSThave beendeclared in anamespace declarationattribute in either the start-tag of the element where the prefix is used or in an ancestor element (i.e. an element in whosecontent theprefixed markup occurs).Furthermore, the attribute value in the innermost such declarationMUST NOTbean empty string.
This constraint may lead to operational difficulties in the case wherethe namespace declaration attribute is provided, not directly in the XMLdocument entity, but via a default attribute declared in an external entity.Such declarations may not be read by software which is based on anon-validating XML processor.Many XML applications, presumably including namespace-sensitive ones, fail torequire validating processors.If correct operation with such applications is required,namespace declarationsMUSTbeprovided either directly or via default attributes declared in theinternal subset of the DTD.
Element names and attributenames are also given as qualified names whenthey appear in declarations in theDTD:
[13] | doctypedecl | ::= | '<!DOCTYPE'SQName (SExternalID)?S? ('[' (markupdecl |PEReference |S)* ']'S?)? '>' |
[14] | elementdecl | ::= | '<!ELEMENT'SQNameScontentspecS? '>' |
[15] | cp | ::= | (QName |choice |seq) ('?' | '*' | '+')? |
[16] | Mixed | ::= | '('S? '#PCDATA'(S? '|'S?QName)*S? ')*' |
| '('S? '#PCDATA'S? ')' | |||
[17] | AttlistDecl | ::= | '<!ATTLIST'SQNameAttDef*S? '>' |
[18] | AttDef | ::= | S (QName |NSAttName)SAttTypeSDefaultDecl |
Note that DTD-based validation is not namespace-aware in the followingsense: a DTD constrains the elements and attributes that may appear ina document by their uninterpreted names, not by (namespace name, localname) pairs. To validate a document that uses namespaces against aDTD, the same prefixes must be used in the DTD as in the instance.A DTD may however indirectly constrain the namespaces used in a validdocument by providing#FIXED
values for attributes thatdeclare namespaces.
The scope of a namespace declaration declaring a prefix extends fromthe beginning of the start-tag in which it appears to the end of thecorresponding end-tag, excluding the scope of any inner declarationswith the same NSAttName part.In the case of an empty tag, the scope is the tag itself.
Such a namespace declaration applies to all element and attributenames within its scope whose prefix matches that specified in thedeclaration.
Theexpanded namecorresponding to a prefixed element or attribute name has theIRI to which theprefixis bound as itsnamespace name,and thelocal partas itslocal name.
<?xml version="1.1"?><html:html xmlns:html='http://www.w3.org/1999/xhtml'> <html:head><html:title>Frobnostication</html:title></html:head> <html:body><html:p>Moved to <html:a href='http://frob.example.com'>here.</html:a></html:p></html:body></html:html>
Multiple namespace prefixes can be declared as attributes of a single element, as shown in this example:
<?xml version="1.1"?><!-- both namespace prefixes are available throughout --><bk:book xmlns:bk='urn:loc.gov:books' xmlns:isbn='urn:ISBN:0-395-36341-6'> <bk:title>Cheaper by the Dozen</bk:title> <isbn:number>1568491379</isbn:number></bk:book>
The attribute value in a namespace declaration for a prefixMAYbe empty.This has the effect, within the scope of the declaration, of removingany association of the prefix with a namespace name. Further declarationsMAYre-declare the prefix again:
<?xml version="1.1"?><x xmlns:n1="http://www.w3.org"> <n1:a/> <!-- legal; the prefix n1 is bound to http://www.w3.org --> <x xmlns:n1=""> <n1:a/> <!-- illegal; the prefix n1 is not bound here --><x xmlns:n1="http://www.w3.org"> <n1:a/> <!-- legal; the prefix n1 is bound again --> </x> </x></x>
The scope of adefault namespace declarationextends from the beginning of thestart-tag in which it appears to the end of the corresponding end-tag,excluding the scope of any inner default namespace declarations.In the case of an empty tag, the scope is the tag itself.
A default namespace declaration applies to all unprefixed element nameswithin its scope.Default namespace declarations do not apply directly to attribute names;the interpretation of unprefixed attributes isdetermined by the element on which they appear.
If there is a default namespace declaration in scope, theexpanded namecorresponding to an unprefixed element name has theIRI of thedefault namespaceas itsnamespace name.If there is no default namespace declaration in scope, thenamespace name has no value.The namespace name for an unprefixed attribute name always has no value.In all cases, thelocal name islocal part(which is of course the same as the unprefixed name itself).
<?xml version="1.1"?><!-- elements are in the HTML namespace, in this case by default --><html xmlns='http://www.w3.org/1999/xhtml'> <head><title>Frobnostication</title></head> <body><p>Moved to <a href='http://frob.example.com'>here</a>.</p></body></html>
<?xml version="1.1"?><!-- unprefixed element types are from "books" --><book xmlns='urn:loc.gov:books' xmlns:isbn='urn:ISBN:0-395-36341-6'> <title>Cheaper by the Dozen</title> <isbn:number>1568491379</isbn:number></book>
A larger example of namespace scoping:
<?xml version="1.1"?><!-- initially, the default namespace is "books" --><book xmlns='urn:loc.gov:books' xmlns:isbn='urn:ISBN:0-395-36341-6'> <title>Cheaper by the Dozen</title> <isbn:number>1568491379</isbn:number> <notes> <!-- make HTML the default namespace for some commentary --> <p xmlns='http://www.w3.org/1999/xhtml'> This is a <i>funny</i> book! </p> </notes></book>
The attribute value in a default namespace declarationMAYbe empty.This has the sameeffect, within the scope of the declaration, of there being no defaultnamespace.
<?xml version='1.1'?><Beers> <!--the default namespace inside tables is that of HTML --> <table xmlns='http://www.w3.org/1999/xhtml'> <th><td>Name</td><td>Origin</td><td>Description</td></th> <tr> <!-- no default namespace inside table cells --> <td><brandName xmlns="">Huntsman</brandName></td> <td><origin xmlns="">Bath, UK</origin></td> <td> <details xmlns=""><class>Bitter</class><hop>Fuggles</hop> <pro>Wonderful hop, light alcohol, good summer beer</pro> <con>Fragile; excessive variance pub to pub</con> </details> </td> </tr> </table> </Beers>
In XML documents conformingto this specification, no tagmay contain two attributes which:
have identical names, or
have qualified names with the samelocal part and withprefixes which have been boundtonamespace names thatareidentical.
This constraint is equivalent to requiring that no element have twoattributes with the sameexpanded name.
For example, each of thebad
start-tags is illegal in thefollowing:
<!-- http://www.w3.org is bound to n1 and n2 --><x xmlns:n1="http://www.w3.org" xmlns:n2="http://www.w3.org" > <bad a="1" a="2" /> <bad n1:a="1" n2:a="2" /></x>
However, each of the following is legal, the second because the default namespace does not apply to attribute names:
<!-- http://www.w3.org is bound to n1 and is the default --><x xmlns:n1="http://www.w3.org" xmlns="http://www.w3.org" > <good a="1" b="2" /> <good a="1" n1:a="2" /></x>
This specification applies to XML 1.1 documents. To conform to thisspecification, a documentMUSTbe well-formed according to the XML 1.1specification[XML 1.1].
In XML documents which conform to this specification, elementand attribute namesMUSTmatch the production forQNameandMUSTsatisfy the "Namespace Constraints". All other tokens in thedocument which areREQUIRED,for XML 1.1 well-formedness, to match theXML production forName,MUSTmatch this specification's production forNCName.
[Definition: A document isnamespace-well-formed if it conforms to this specification.]
It follows that in a namespace-well-formed document:
All element and attribute names contain either zero or one colon;
No entity names, processing instruction targets, or notation names contain any colons.
In addition, a namespace-well-formed document may also be namespace-valid.
[Definition: A namespace-well-formed document isnamespace-validif it is valid according to the XML 1.1 specification, and all tokensother than element and attribute names which areREQUIRED,for XML 1.1 validity, to match the XML production forName,match this specification's production forNCName.]
It follows that in a namespace-valid document:
No attributes with a declared type ofID,IDREF(S),ENTITY(IES), orNOTATION contain any colons.
To conform to this specification, a processorMUSTreportviolations of namespace well-formedness, with the exception that itis notREQUIREDto check that namespace names are legal IRIs.
[Definition: A validating XML processor that conforms to this specificationisnamespace-validating if in additionit reports violations of namespace validity.]
Work is currently in progress to produce an RFC definingInternationalized Resource Identifiers (IRIs). Since this work is notyet complete, this section gives a syntactic definition of IRIsfor the purposes of this specification. The XML Core Working Group expectsto issue an erratumreplacing this section with a reference to the RFC when it ispublished.
Users defining namespaces are advised to restrict namespacenames to URIs until the RFC is published and software supportingIRIs is in common use. Implementors are likewise advised not to rejectnamespace names that violate the drafts in terms of the allowed characters.
For a more general definition and discussion of IRIs see[IRI draft 5] (work in progress).
URI references are restricted to a subset of the ASCII characters;IRI references allow most Unicode characters from #xA0 onwards.Earlier drafts of the IRI RFC (eg[IRI draft 3]) alsoallowed some of the disallowed ASCII characters, but the current draft([IRI draft 5]) does not.
[Definition: Theadditional characters allowed in IRIs by[IRI draft 5]are:]
the Unicode plane 0 characters #xA0 - #xD7FF, #xF900-#xFDCF, #xFDF0-#xFFEF
the Unicode plane 1-14 characters #x10000-#x1FFFD ... #xD0000-#xDFFFD, #xE1000-#xEFFFD
[Definition: AnIRI reference is a string that can be converted toa URI reference by applying the following steps:]
Convert the hostname part, if present, using the ToASCII operation specified in Section 4.1 of[RFC3490] with the flags UseSTD3ASCIIRules and AllowUnassigned set to TRUE.
Escape alladditional characters as follows:
Each additional character is converted to UTF-8[RFC3629] as one or more bytes.
The resulting bytes are escaped with the URI escaping mechanism (that is, converted to %HH, where HH is the hexadecimal notation of the byte value).
The original character is replaced by the resulting character sequence.
Note:
The algorithm in[IRI draft 5] includes a UCS normalizationstep, but this makes no difference to which strings are IRI references.
This version incorporates the errata to version 1.0 as of 6 December 2002[1.0 Errata]. There are two further substantive changes:
A mechanism is provided for undeclaring prefixes;
Namespace names are IRIs, rather than URIs.
There are several editorial changes, including a numberof terminology changes and additions intended to produce greaterconsistency. The non-normative appendix "The Internal Structure of XML Namespaces" has been removed.
This work reflects input from a very large number of people, including especially the participants in the World WideWeb Consortium XML Working Group and Special Interest Group and the participants in the W3C Metadata Activity.The contributions of Charles Frankston of Microsoft were particularly valuable.