Please refer to theerrata for this document, which may include normative corrections.
See alsotranslations.
This document is also available in these non-normative formats:XML and HTML highlightingdifferences from the second edition.
Copyright © 2009 W3C® (MIT,ERCIM,Keio), All Rights Reserved. W3Cliability,trademark anddocument use rules apply.
XML namespaces provide a simple method for qualifying element and attributenames used in Extensible Markup Language documents by associating themwith namespaces identified by URI 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 indexat http://www.w3.org/TR/.
This document is a product of theXML Core Working Groupas part 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-names.
Known implementations are documented in theNamespaces 1.1 implementation report(all known Namespaces 1.1 implementations also support Namespaces 1.0).A test suite is also available via theXML Test Suitepage.
This third edition incorporates all known errata as of the publication date.It supersedes the previousedition of 16 August 2006.
This edition has been widely reviewed. Only minor editorial changes have been made since the 6 August 2009 Proposed Edited Recommendation.
Please report errors in this document toxml-names-editor@w3.org;publicarchivesare available. The errata list for this document is available athttp://www.w3.org/XML/2009/xml-names-errata.
This document has been reviewed by W3C Members, by software developers, and by other W3C groups and interested parties, and is endorsed by the Director as a W3C Recommendation. It is a stable document and may be used as reference material or cited from another document. W3C's role in making the Recommendation is to draw attention to the specification and to promote its widespread deployment. This enhances the functionality and interoperability of the Web.
W3C maintains apubliclist of any patent disclosuresmade in connection with the deliverables ofthe group; that page also includes instructions for disclosing a patent.An individual who has actual knowledge of a patent which the individualbelieves containsEssentialClaim(s)must disclose the information in accordance withsection6 of the W3C Patent Policy.
1Motivation and Summary
1.1A Note on Notation and Usage
2XML Namespaces
2.1Basic Concepts
2.2Use ofURIsas Namespace Names
2.3ComparingURIReferences
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
ANormative References
BOther references (Non-Normative)
CThe Internal Structure of XML Namespaces (Non-Normative)
DChanges since version 1.0 (Non-Normative)
EAcknowledgements (Non-Normative)
FOrphaned Productions (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 bya URI reference[RFC3986];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 identifiedbya URII, 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 URI namespacewith the vocabulary's local names that is effective in avoidingname clashes.
URIreferences 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 legalURIreference, cannot be used as a namespace name.
The use of relativeURIreferences,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".
URIreferences identifying namespaces are compared when determiningwhether a name belongs to a given namespace, and whether two namesbelong to the same namespace.[Definition: The twoURIsare 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 thatURIreferences which are not identicalin this sense may resolve to the same resource. Examples includeURIreferences which differ only in case or %-escaping, or which arein external entities which have different base URIs (but note thatrelativeURIsare deprecated as namespace names).
In a namespace declaration, theURIreference is thenormalized valueof the attribute, so replacement of XML character and entity referenceshas already been done before any comparison.
Examples:
TheURIreferences 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 URI references below are also all different for the purposes of identifyingnamespaces:
http://www.example.org/~wilbur
http://www.example.org/%7ewilbur
http://www.example.org/%7Ewilbur
Because of the risk of confusion betweenURIsthat 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 | ::= | Name - (Char* ':'Char*) | /* An XMLName, minus the ":" */ |
The attribute'snormalized valueMUSTbe eithera URIreference — 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 NOTbebound to any other namespace name. Other prefixesMUST NOTbe bound to this namespacename,and itMUST NOTbe declared as the default namespace.
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.Other prefixesMUST NOTbe bound to this namespace name,and itMUST NOTbe declared as the default namespace.Element namesMUST NOThave the prefixxmlns
.
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:
[7] | QName | ::= | PrefixedName |
|UnprefixedName | |||
[8] | PrefixedName | ::= | Prefix ':'LocalPart |
[9] | UnprefixedName | ::= | LocalPart |
[10] | Prefix | ::= | NCName |
[11] | LocalPart | ::= | NCName |
ThePrefix provides thenamespace prefixpart of the qualified name, andMUSTbe associated with a namespaceURIreference 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:
[12] | STag | ::= | '<'QName (SAttribute)*S? '>' | [NSC: Prefix Declared] |
[13] | ETag | ::= | '</'QNameS? '>' | [NSC: Prefix Declared] |
[14] | 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:
[15] | Attribute | ::= | NSAttNameEqAttValue | |
|QNameEqAttValue | [NSC: Prefix Declared] | |||
[NSC: No Prefix Undeclaring] | ||||
[NSC: Attributes Unique] |
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).
Namespace constraint: No Prefix Undeclaring
In anamespace declaration for aprefix (i.e., where theNSAttName is aPrefixedAttName), theattribute valueMUST NOT be empty.
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:
[16] | doctypedecl | ::= | '<!DOCTYPE'SQName (SExternalID)?S? ('[' (markupdecl |PEReference |S)* ']'S?)? '>' |
[17] | elementdecl | ::= | '<!ELEMENT'SQNameScontentspecS? '>' |
[18] | cp | ::= | (QName |choice |seq) ('?' | '*' | '+')? |
[19] | Mixed | ::= | '('S? '#PCDATA'(S? '|'S?QName)*S? ')*' |
| '('S? '#PCDATA'S? ')' | |||
[20] | AttlistDecl | ::= | '<!ATTLIST'SQNameAttDef*S? '>' |
[21] | 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 theURIto which theprefixis bound as itsnamespace name,and thelocal partas itslocal name.
<?xml version="1.0"?><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.0"?><!-- 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 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 theURIof 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.0"?><!-- 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.0"?><!-- 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.0"?><!-- 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.0'?><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>
Namespace constraint: Attributes Unique
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
empty-element 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.0documents. To conform to thisspecification, a documentMUSTbe well-formed according to the XML 1.0 specification[XML].
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.0 well-formedness, to match theXML production forNameMUSTmatch 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.0 specification, and all tokensother than element and attribute names which areREQUIRED,for XML 1.0 validity, to match the XML production forNamematch 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 URI references[RFC3986].
[Definition: A validating XML processor that conforms to this specificationisnamespace-validating if in additionit reports violations of namespace validity.]
This version incorporates the errata as of 20 July 2009[1.0 Errata][1.0 2e Errata].
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. The BNFhas been adjusted to interconnect properly with all editions of XML 1.0,including the fifth edition.
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.
The following two productions are modified versions of ones which were present in the first two editions of this specification. They are no longer used, but are retained here to satisfy cross-references to undated versions of this specification.
Because theLetter
production of XML 1.0, originally used in the definition ofNCNameStartChar
, is no longer the correct basis for defining names since XML 1.0 Fifth Edition, theNCNameStartChar
production has been modified to give the correct results against any edition of XML, by definingNCNameStartChar
in terms ofNCName.
[5] | NCNameChar | ::= | NameChar- ':'/* An XMLNameChar, minus the ":" */ |
[6] | NCNameStartChar | ::= | NCName - (CharCharChar* )/* The first letter of anNCName */ |
Note:
ProductionNC-NCNameStartChar takes advantage of the fact that a single-character NCName is necessarily an NCNameStartChar, and works by subtracting from the set of NCNames of all lengths the set of all strings of two or more characters, leaving only the NCNames which are one character long.