Namespaces
Jean-François Perrot
- Principle
- Syntax
- Colon as
separator
- Shorthand :
prefixes
- Default
namespace
- Examples
- Namespaces in DOM-Java
- Documents created by a
DocumentBuilder (parser)
- Empty Documents created by a
DOMImplementation
- DOM manipulation of namespace aware
documents
-
- Names of tags and
of attributes do carry meaning, hence
they are ambiguous
- there is no such thing as a word having a single meaning...
- the same problem appears in many places, e.g. in programming languages.
- Namespaces
are a way to resove ambiguity.
A (full) name is made up of 2 parts :
- a local part, supposed to be unambiguous inside a certain
domain called namespace :
e.g. "title
" or "head
" or "body
" in the domain "XHTML"
- a name for the domain, supposed to absolutely unambiguous : is this possible ?
- URIs are supposed to be unambiguous : they will be
used as namespace names.
e.g. "http://www.w3.org/1999/xhtml"
for the domain "XHTML"
Note that in this function URIs are pure symbols, they do not
point to any resource, they are non-dereferenceable.
If they do point to some resource, this resource has no specific property : e.g. "http://www.w3.org/1999/xhtml"
-
-
A full name (or qualified
name) is written with (1) the
namespaee (2) the local name, separated by a colon.
e.g. two different notions of title
http://www.w3.org/1999/xhtml:title
(title
in XHTML)
http://purl.org/dc/elements/1.1/:title
(title
according to the Dublin
Core Metadata)
-
- Namespace URIs
are seldom used directly (too long, making the document illegible).
They are ususally abbreviated as prefixes
Note that these prefixes exist only in the text that represents the
document as a file,
in the (DOM object) document itself only URIs are known.
- Prefixes are
declared alongside attributes in an opening
tag,
in the form : xmlns:prefix = URI
They remain valid for all chidren of the tag.
Very often the tag is the root tag of the document...
e.g.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
Caution : namespace prefixes are NOT attributes, although they look the same and appear in the same places !
- The '
xml
' prefix is supposed to be implicitly defined in every XML document.
The corresponding URL is http://www.w3.org/XML/1998/namespace
.
- Example
with 2 namespace prefixes
rdf
and epita
:
See here for
background about the RDF formalism and here
about the names & marks data set.
File rNN2.rdf
<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:epita="http://epita/masters/international/"
xml:base="http://epita/masters/international/perso">
<rdf:Description
rdf:about="#Elisabeth"><epita:mark>07</epita:mark></rdf:Description>
<rdf:Description
rdf:about="#Luc"><epita:mark>12</epita:mark></rdf:Description>
<rdf:Description
rdf:about="#Maurice"><epita:mark>18</epita:mark></rdf:Description>
<rdf:Description
rdf:about="#Juliette"><epita:mark>07</epita:mark></rdf:Description>
</rdf:RDF>
Note that
xmlns:rdf
and xmlns:epita
are not attributes of the RDF tag.
xml:base
is an attribute,
the name of which belongs to the predefined xml
namespace.
- In this
example, both tags and attributes
names carry a namespace prefix.
Usually, only tags carry namespaces.
This point will reappear when we will specify this using XML Schemas.
-
- Empty prefix:
Very often, most names in a document belong
to the same namespace.
As a further shorthand, this namespace may be represented by an empty
prefix, without colon.
It is then called the default namespace.
Ex. XHTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html;
charset=UTF-8" http-equiv="content-type" />
<title>M2-IM</title>
<style type="text/css">....
- But note that
this is merely a convenience for making the
text easier to read.
From the point of view of the (DOM) document, the default namespace is
a namespace like all others...
Indeed, quite often using an empty prefix is prohibited !
However, some pieces of software may think otherwise : most browsers won't interpret properly our basic example below
it it is written with a non-empty prefix for html
...
To make this more precise, note that the defaut namespace applies to tags
(elements) only,
the namespace of attributes being determined by the element on
which they appear [Ref.].
However, some disagree with this interpretation : the JavaDoc on DOMNode:getNamespaceURI
says:
Note: Per the Namespaces in XML
Specification [XML Namespaces]
an attribute does not inherit its namespace from the element it is
attached to. If an attribute is not explicitly given a namespace, it
simply has no namespace.
- Basic
example
<?xml version="1.0"
encoding='ISO-8859-1'?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:xdc="http://www.xml.com/books">
<head><title>Book Review</title></head>
<body>
<xdc:bookreview>
<h1><xdc:title style="font-family:
sans-serif;">XML: A Primer</xdc:title></h1>
<table>
<tr align="center">
<td>Author</td><td>Price</td>
<td>Pages</td><td>Date</td>
</tr>
<tr align="left">
<td><xdc:author>Simon St.
Laurent</xdc:author></td>
<td><xdc:price>31.98</xdc:price></td>
<td><xdc:pages>352</xdc:pages></td>
<td><xdc:date>1998/01</xdc:date></td>
</tr>
</table>
</xdc:bookreview>
</body>
</html>
-
- Namespaces are
associated with all major dialects of XML.
- XHTML :
<html
xmlns="http://www.w3.org/1999/xhtml">
- XSLT :
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" ... >
- XSL-FO :
<fo:root
xmlns:fo="http://www.w3.org/1999/XSL/Format">
- XML-Schemas :
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
not to be confused with xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- RNG
Grammars :
<grammar
xmlns="http://relaxng.org/ns/structure/1.0">
- RDF :
<rdf:RDF
xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>
- SPARQL results
:
<sparql
xmlns="http://www.w3.org/2005/sparql-results#">
- DocBook :
<book xml:id="simple_book" xmlns="http://docbook.org/ns/docbook" ... >
- etc...
- Typical example
from SOAP-based web services : wsdl, request
& response
-
By default DOM-Java
ignores namespaces, i.e. DOM documents are not namespace
aware.
- This does not
create problems if only reading is
involved : see
Average_R
.
- Same for building if only one namespace is involved
and only printout : see
One2HtmlT
.
But if more complex manipulations are used (XSLT transforms, validity
checking), then
in order to properly manage documents with namespaces, some precautions
are necessary.
-
A
DocumentBuilder
p
parses a file to create a Document
doc
: we want doc
to be namespace aware.
Then p
must have been created by a DocumentBuilderFactory
which was namespace aware.
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true); // default is
"false"
DocumentBuilder p = dbf.newDocumentBuilder();
Document doc_xsl = p.parse(myFile.xml);
-
The
DOMImplementation
object was obtained from a DocumentBuilder
,
that must have been created by a DocumentBuilderFactory
which was namespace aware.
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DOMImplementation domi =
dbf.newDocumentBuilder().getDOMImplementation();
Document doc = domi.createDocument(nmsURI, root,
null);
where
root
is the qualified name
of
the root tag of the document
nmsURI
is the corresponding
namespace URI.
- the third
argument (if not
null
) is the DocumentType
,
i.e. a Java object representing the DTD.
Note that the specification that root
be a QName
(= prefix:localName
) means that a prefix for the
namespace is implicitly declared. A document like our RDF example would
have been created as
String nmsURI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
Document doc = domi.createDocument (nmsURI, "rdf:RDF", null);
The namespace will implicitly be declared as default by writing
root
without a prefix.
A document like our createElement(String tagName) Basic Example would
have been created as
String nmsURI = "http://www.w3.org/1999/xhtml";
Document doc = domi.createDocument (nmsURI, "html", null);
-
A number of heavily
used DOM methods that take a name argument
have counterpart "with namespace", e.g.
- in
Document
: createElement(String
tagName)
--> createElementNS(String
namespaceURI, String qualifiedName)
- in
Element
: getAttribute(String
name)
--> getAttributeNS(String namespaceURI, String
localName)
- id.
setAttribute(String
name, String value)
--> setAttributeNS(String namespaceURI, String qualifiedName,
String value)
- id.
getElementsByTagName(String
name)
--> getElementsByTagNameNS(String namespaceURI, String
localName)
These methods belong the the 2nd level of the DOM specification (DOM level 2).
Example : BuildRfromText
.
We wiil soon meet situations
where they must be used !