Title: Namespace in XML
1Namespace in XML
2Abstract
- XML namespaces provide a simple method for
qualifying element and attribute names used in
XML documents by associating them with namespaces
identified by URI references.
3Table of Contents
- 0. Quick views
- 1. Motivation and Summary
- 2. Declaring Namespaces
- 3. Qualified Names
- 4. Using Qualified Names
- 5. Applying Namespaces to Elements and Attributes
- 5.1 Namespace Scoping
- 5.2 Namespace Defaulting
- 5.3 Uniqueness of Attributes
- 6. Conformance of Documents
40. quick views Motivation
- name clashes.
- Consider an XML language WidgetML which uses
XHTML as a sublanguage for help - messages
- ltwidget type"gadget"gt
- lthead size"medium"/gt
- ltbodygtltsubwidget ref"gizmo"/gtlt/biggt
- ltinfogt ltheadgtlttitlegtDescription of
gadgetlt/titlegt - lt/headgt ltbodygt lth1gtGadgetlt/h1gt
- A gadget contains a big gizmo
- lt/bodygt
- lt/infogtlt/widgetgt
- Meaning of head and body depends on context!
- complicates things for processors and might even
cause ambiguities. - The solution multiple namespaces.
- name ltnamespace name, local namegt
5Qualifying names
- lexical rules for namespace and local names
- require every namespace to be a URIs (Universal
Resource Identifiers) called its namespace URI. - gt name namespace URIlocal part
- http//www.w3.org/TR/xhtml1head
- namespace URI
local part - same with attribute names.
- Notes
- URI URL ? URN (extended to IRI (W3c draft) at
1.1). - URI here used only for identification - doesn't
have to point at anything. - Not all URIs are legal Attribute/element names.
(XML names do not allow/restrict the use of
special characters - (._- ?ok) (/,,, ?no)
- use namespace prefix as a proxy for namespace URI.
6Namespace declarations
- indirection namespaces are declared by special
namespace attributes (xmlns or xmlns) and
associated prefixes. - Example
- lt... xmlnsfoo"http//www.w3.org/TR/xhtml1"gt
- ...
- ltfooheadgt...lt/fooheadgt
- ...
- lt/...gt
- xmlnsprefix"URI" declares a namespace with a
prefix and a URI. - Scope rule lexical
- the element containing the declaration and all
descendants - can be overridden by nested declaration
- Both element and attribute names can be qualified
with namespaces. - Note the prefix is just a proxy - applications
should use only the URI for identification.
7The default namespace
- for backward compatibility and simplicity.
- declaration xmlnsaURI"
- Unprefixed element names are assigned the default
namespace aURI. - default value "" (means disable default
namespace) - or xmlns ""
- Ex
- ltex xmlnshttp//test.com/ att1
xmlnsshttp//test.com/gt - ltex att1abc gt lt/ex1gt
- ltex xmlns satt1abcgtlt/ex1gt
- lt/examplegt
- Default namespace affects element names only it
does not affect unprefixed attribute names. - in Ex 1. the 1st and 2nd ltexgt belong to the same
namespace ! namespace URI of 3rd ltexgt - 2. all ltattgtattributes belong to the same
namespace.
8An example WidgetML with namespaces
- ltwidget xmlns"http//www.widget.org"
- xmlnsxhtml"http//www.w3.org/TR/xhtml1"
- type"gadget"gt
- lthead size"medium"/gt
- ltbodygtltsubwidget ref"gizmo"/gtlt/biggt
- ltinfogtltxhtmlheadgt
- ltxhtmltitlegtDescription of
gadgetlt/xhtmltitlegt - lt/xhtmlheadgt
- ltxhtmlbodygt ltxhtmlh1gtGadgetlt/xhtmlh1gt
- A gadget contains a big gizmo
- lt/xhtmlbodygt lt/infogtlt/widgetgt
- The main part of WidgetML uses the default
namespace which has the URI http//www.widget.org
- XHTML uses the namespace prefix xhtml which is
assigned the URI http//www.w3.org/TR/xhtml1.
9Consequences for other XML technologies
- - namespace awareness.
- XML languages and applications should consider
Namespaces as an inherent part of XML. - DTD should take namespace into considerations
- parsers
- need an extra processing layer on top to resolve
namespace declarations and uses - schemas
- schemas should be used to declare all possible
element/attribute names of a namespace. - namespace URIs could be tied together with
schemas -
- ...
101. Summary
- Definition An XML namespace is a collection of
names, identified by a URI reference RFC2396,
which are used in XML documents as element types
and attribute names. - Definition URI references which identify
namespaces are considered identical when they are
exactly the same character-for-character. - Examples include URI references which differ
only in case, or which are in external entities
which have different effective base URIs. - E.g. http//a.b.c/wine/d http//a.B.c/7Ewine/d
- http//a.b.c/7ewine/d d (relative URI
deprecated)
111. Summary (contd)
- Names from XML namespaces appear as
- qualified names,
- which contain a single colon, separating the name
into a namespace prefix and a local part. -
- URI references can contain characters not allowed
in names, so cannot be used directly as namespace
prefixes. Therefore, the namespace prefix serves
as a proxy for a URI reference. - attribute-based syntax used to declare the
association of the namespace prefix with a URI
reference.
122. Declaring Namespaces
- Definition A namespace is declared using a
family of reserved attributes. - Such an attribute's name must either be xmlns or
have xmlns as a prefix. - These attributes, like any other XML attributes,
may be provided directly or by default.
13Attribute Names for Namespace Declaration
- 1 NSAttName PrefixedAttName
DefaultAttName - 2 PrefixedAttName 'xmlns' NCName
- NSC Leading "XML"
- 3 DefaultAttName 'xmlns'
- 4 NCName (Letter '_') (NCNameChar)
- / An XML Name, minus the "" /
- 5 NCNameChar Letter Digit '.' '-'
'_' - CombiningChar
Extender - Note NCNames are legal XML Name containing no
char . - ltx xmlnsedi'http//ecommerce.org/schema'gt
- lt!-- the "edi prefix is bound to
http//ecommerce.org/schema for the "x" element
and contents --gt - lt/xgt
142. Declaring Namespaces (contd)
- Definition The attribute's value, a URI
reference, is the namespace name identifying the
namespace. - The namespace name should have the
characteristics of uniqueness and persistence. - It is not a goal that it be directly usable for
retrieval of a schema (if any exists). - syntax designed with these goals Uniform
Resource Names RFC2141. - Ordinary URLs can be managed in such a way as to
achieve these same goals.
152. Declaring Namespaces (contd)
- Definition If the attribute name matches
PrefixedAttName, then the NCName gives the
namespace prefix, used to associate element and
attribute names with the namespace name in the
attribute value. - In such declarations, the namespace name may not
be empty. - Definition If the attribute name matches
DefaultAttName, then the namespace name in the
attribute value is that of the default namespace.
- In such a default declaration, the attribute
value may be empty. ? Disable default namespace.
163. Qualified Names
- Definition In XML documents, some names may be
given as qualified names, defined as follows - Qualified Name
- 6 QName (Prefix '')? LocalPart
- 7 Prefix NCName
- 8 LocalPart NCName
- The Prefix provides the namespace prefix part of
the qualified name, and must be associated with a
namespace URI reference in a namespace
declaration. - Note that the prefix functions only as a
placeholder for a namespace name. Applications
should use the namespace name, not the prefix,in
constructing names whose scope extends beyond the
containing document.
174. Using Qualified Names for element type
- Element types are given as qualified names, as
follows - Element Types
- 9 STag 'lt' QName (S Attribute) S? 'gt'
- 10 ETag 'lt/' QName S? 'gt'
- 11 EmptyElemTag 'lt' QName (S Attribute)
S? '/gt' - NSC Prefix Declared
- Example
- ltx xmlnsedi'http//ecommerce.org/schema'gt
- lt!-- the 'price' element's namespace is
http//ecommerce.org/schema --gt - ltediprice units'Euro'gt32.18lt/edipricegt
- lt/xgt
184. Using Qualified Names for attribute names
- Attributes are either namespace declarations or
their names are given as qualified names - Attribute
- 12 Attribute NSAttName Eq AttValue
- QName Eq
AttValue - NSC Prefix Declared
- Example of a qualified name serving as an
attribute name - ltx xmlnsedi'http//ecommerce.org/schema'gt
- lt!-- the 'taxClass' attribute's namespace is
http//ecommerce.org/schema --gt - ltlineItem editaxClass"exempt"gtBaby
foodlt/lineItemgt - lt/xgt
194. Using Qualified Names in DTD
- Element names and attribute types are also given
as qualified names in the DTD - Qualified Names in Declarations
- 13 doctypedecl 'lt!DOCTYPE' S QName (S
ExternalID)? - S? ('' (markupdecl PEReference
S) '' S?)? 'gt' - 14 elementdecl 'lt!ELEMENT' S QName S
contentspec - S? 'gt'
- 15 cp (QName choice seq) ('?'
'' '')? - 16 Mixed '(' S? 'PCDATA' (S? '' S?
QName) S? ')' - '(' S? 'PCDATA' S?
')' - 17 AttlistDecl 'lt!ATTLIST' S QName AttDef
S? 'gt' - 18 AttDef S (QName NSAttName) S AttType S
- DefaultDec
205. Applying Namespaces to Elements and Attributes
- 5.1 Namespace Scoping
- The namespace declaration is considered to apply
to the element where it is specified and to all
elements within the content of that element,
unless overridden by another namespace
declaration with the same NSAttName part. - Example
- lt?xml version"1.0"?gt
- lt!-- all elements here are explicitly in the HTML
namespace --gt - lthtmlhtml xmlnshtml'http//www.w3.org/TR/REC-ht
ml40'gt - lthtmlheadgtlthtmltitlegtFrobnosticationlt/htmltitle
gtlt/htmlheadgt - lthtmlbodygtlthtmlpgtMoved to lthtmla
href'http//frob.com'gthere.lt/htmlagtlt/htmlpgtlt/ht
mlbodygt - lt/htmlhtmlgt
215.1 Namespace Scoping
- Multiple namespace prefixes can be declared as
attributes of a single element. - Example
- lt?xml version"1.0"?gt
- lt!-- both namespace prefixes are available
throughout --gt - ltbkbook xmlnsbk'urnloc.govbooks'
- xmlnsisbn'urnISBN0-395-36341-6'gt
- ltbktitlegtCheaper by the Dozenlt/bktitlegt
- ltisbnnumbergt1568491379lt/isbnnumbergt
- lt/bkbookgt
225.2 Namespace Defaulting
- A default namespace is applied to
- the element where it is declared (if that element
has no namespace prefix), and to - all elements with no prefix within the content of
that element. - If the URI reference in a default namespace
declaration is empty, then unprefixed elements
are not considered to be in any namespace. - Note
- Default namespaces do not apply directly to
attributes !!!
235.2 Namespace Defaulting examples
- lt?xml version"1.0"?gt
- lt!-- elements are in the HTML namespace, by
default --gt - lthtml xmlns'http//www.w3.org/TR/REC-html40'gt
- ltheadgtlttitlegtFrobnosticationlt/titlegtlt/headgt
- ltbodygtltpgtMoved to
- lta href'http//frob.com'gtherelt/agt.lt/pgtlt/body
gt - lt/htmlgt
- lt?xml version"1.0"?gt
- lt!-- unprefixed element types are from "books"
--gt - ltbook xmlns'urnloc.govbooks'
- xmlnsisbn'urnISBN0-395-36341-6'gt
- lttitlegtCheaper by the Dozenlt/titlegt
- ltisbnnumbergt1568491379lt/isbnnumbergt
- lt/bookgt
245.2 Namespace Defaulting a larger example
- lt?xml version"1.0"?gt
- lt!-- initially, the default namespace is "books"
--gt - ltbook xmlns'urnloc.govbooks'
- xmlnsisbn'urnISBN0-395-36341-6'gt
- lttitlegtCheaper by the Dozenlt/titlegt
- ltisbnnumbergt1568491379lt/isbnnumbergt
- ltnotesgt
- lt!-- make HTML the default namespace for
some commentary --gt - ltp xmlns'urnw3-org-nsHTML'gt
- This is a ltigtfunnylt/igt book!
- lt/pgt
- lt/notesgt
- lt/bookgt
255.2 Namespace Defaulting
- The default namespace can be set to the empty
string to disable default namespace. - lt?xml version'1.0'?gt ltBeersgt
- lt!-- the default namespace is now that of HTML
--gt - lttable xmlns'http//www.w3.org/TR/REC-html40'gt
- ltthgtlttdgtNamelt/tdgtlttdgtOriginlt/tdgtlttdgtDescriptio
nlt/tdgtlt/thgt - lttrgt lt!-- no default namespace inside table
cells --gt - lttdgt ltbrandName xmlns" gtHuntsmanlt/brandNam
egt lt/tdgt - lttdgt ltorigin xmlns" gtBath, UKlt/origingt
lt/tdgt - lttdgt ltdetails xmlns" gtltclassgtBitterlt/clas
sgtlthopgtFuggleslt/hopgt - ltprogtWonderful hop, light alcohol,
good summer beerlt/progt - ltcongtFragile excessive variance
pub to publt/congt - lt/detailsgt lt/tdgt lt/trgt
- lt/tablegt lt/Beersgt
265.3 Uniqueness of Attributes
- In XML documents conforming to this
specification, no tag may contain two attributes
which - 1.have identical names, or // for well-formedness
- 2.have qualified names with the same local part
and with prefixes which have been bound to
namespace names that are identical. // for
namespace conformity - Example, each of the following bad start-tags is
illegal - lt!-- http//www.w3.org is bound to n1 and n2 --gt
- ltx xmlnsn1"http//www.w3.org"
- xmlnsn2"http//www.w3.org" gt
- ltbad a"1" a"2" /gt
- ltbad n1a"1" n2a"2" /gt
- lt/xgt
275.3 Uniqueness of Attributes (contd)
- Each of the following is legal, the second is so
because the default namespace does not apply to
attribute names! - lt!-- http//www.w3.org is bound to n1 and is the
default --gt - ltx xmlnsn1"http//www.w3.org"
- xmlns"http//www.w3.org" gt
- ltgood a"1" b"2" /gt
- ltgood a"1" n1a"2" /gt
- lt/xgt
286. Conformance of Documents
- In XML documents which conform to this
specification, - element types and attribute names must match the
production for QName and must satisfy the
"Namespace Constraints". - all other tokens in the document which are
required to match the XML production for Name
match this specification's production for NCName.
- The effect of conformance is that in such a
document - All element types and attribute names contain
either zero or one colon. - No entity names, PI targets, or notation names
contain any colons.
29A.2 XML Namespace Partitions (removed in 1.1)
- Each XML namespace contains disjoint partitions
- 1. The All Element Types Partition
- combination of the namespace name and the local
part uniquely identifies the element type. - 2. The Global Attribute Partition
- The combination of the namespace name and the
attribute name uniquely identifies the global
attribute. - 3. The Per-Element-Type (local attribute)
Partitions - The combination of the attribute name with the
element's type and namespace name uniquely
identifies each unqualified attribute.
30Examples of namespace partitions
- lt!-- 1 --gt ltsection xmlns'urncombooks-r-us'gt
- lt!-- 2 --gt lttitlegtBook-Signing Eventlt/titlegt
- lt!-- 3 --gt ltsigninggt
- lt!-- 4 --gt ltauthor title"Mr" name"Vikram
Seth" /gt - lt!-- 5 --gt ltbook title"A Suitable Boy"
price"22.95" /gt - lt/signinggt lt/sectiongt
- Note Titles in 4 and 5 belong to different
partitions - lt!-- 1 --gt ltRESERVATION xmlnsHTML"http//www.w3.
org/TR/REC-html40"gt - lt!-- 2 --gt ltNAME HTMLCLASS"largeSansSerif"gtLaym
an, Alt/NAMEgt - lt!-- 3 --gt ltSEAT CLASS"Y" HTMLCLASS"largeMonot
ype"gt33Blt/SEATgt - lt!-- 4 --gt ltHTMLA HREF'/cgi-bin/ResStatus'gtChec
k Statuslt/HTMLAgt - lt!-- 5 --gt ltDEPARTUREgt1997-05-24T0755001lt/DEPA
RTUREgt - lt/RESERVATIONgt
- Notes 1. two namespace default and HTML.
31Links to more information
- http//www.w3.org/TR/REC-xml-names
- The W3C XML Namespace Recommendation v1.0
- http//www.w3.org/TR/xml-names11/
- Namespaces in XML 1.1 W3C Candidate
Recommendation 18 December 2002 - IRI draft
- Internationalized Resource Identifiers (IRIs).
Available at http//www.w3.org/International/iri-e
dit/draft-duerst-iri-02.txt - RFC2396
- IETF (Internet Engineering Task Force) RFC 2396
Uniform Resource Identifiers (URI) Generic
Syntax, eds. T. Berners-Lee, R. Fielding, L.
Masinter. August 1998. Available at
ftp//ftp.ietf.org/rfc/rfc2396.txt