To distinguish between elements and attributes from different vocabularies with different meanings.
To group all related elements and attributes together so that a parser can easily recognize them.
The XLink specification defines an attribute with the name href
.
The XHTML specification also uses href
attributes on some elements.
And the XInclude specification uses href
attributes.
An XSLT style sheet that will transform XHTML documents containing both Scalable Vector Graphics (SVG) pictures and MathML equations into XSL-Formatting object documents.
The a
, title
, script
,
style
and font
elements in XHTML and SVG
The table
element in XHTML and XSL-FO
The text
element in XSLT and SVG
The set
element in MathML and SVG
An XSLT stylesheet that transforms a style sheet in an older version of the XSLT specification to a style sheet in a newer version of the XSLT specification.
Namespaces disambiguate elements with the same name from each other by attaching different prefixes to names from different XML applications.
Each prefix is associated with a URI.
Names whose prefixes are associated with the same URI are in the same namespace.
Names whose prefixes are associated with different URIs are in different namespaces.
Uniform Resource Identifier
Two kinds:
URLs: Uniform Resource Locators
URNs: Uniform Resource Names
Which should be used for namespaces?
Absolute vs. relative
Elements and attributes that are in namespaces have names that contain exactly one colon. They look like this:
rdf:description
xlink:type
xsl:template
Everything before the colon is called the prefix
Everything after the colon is called the local part.
The complete name including the colon is called the qualified name.
Each prefix is associated with a URI (technically a URI reference) called a namespace name.
For example, all elements in XSLT 1.0 style sheets are associated with the http://www.w3.org/1999/XSL/Transform URI.
The customary prefix xsl
is a shorthand for the longer URI
http://www.w3.org/1999/XSL/Transform.
You can't use the URI in the element name directly.
{http://www.w3.org/1999/XSL/Transform}template
http://www.w3.org/1999/XSL/Transform#template
Prefixes are bound to namespace URIs by attaching an xmlns:prefix
attribute to the prefixed element or one of its ancestors.
<svg:svg xmlns:svg="http://www.w3.org/2000/svg"
width="12cm" height="10cm">
<svg:ellipse rx="110" ry="130" />
<svg:rect x="4cm" y="1cm" width="3cm" height="6cm" />
</svg:svg>
Bindings have scope within the element where they're declared.
An SVG processor recognizes all three of these elements as SVG elements because they all have prefixes bound to the particular URI defined by the SVG specification.
<xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:mathml="http://www.w3.org/1998/Math/MathML">
<xhtml:head><xhtml:title>Three Namespaces</xhtml:title></xhtml:head>
<xhtml:body>
<xhtml:h1 align="center">An Ellipse and a Rectangle</xhtml:h1>
<svg:svg xmlns:svg="http://www.w3.org/2000/svg"
width="12cm" height="10cm">
<svg:ellipse rx="110" ry="130" />
<svg:rect x="4cm" y="1cm" width="3cm" height="6cm" />
</svg:svg>
<xhtml:p>The equation for ellipses</xhtml:p>
<mathml:math>
<mathml:apply>
<mathml:eq/>
<mathml:cn> 1 </mathml:cn>
<mathml:apply>
<mathml:plus/>
<mathml:apply>
<mathml:divide/>
<mathml:apply>
<mathml:power/>
<mathml:ci> x </mathml:ci>
<mathml:cn> 2 </mathml:cn>
</mathml:apply>
<mathml:apply>
<mathml:power/>
<mathml:ci> a </mathml:ci>
<mathml:cn> 2 </mathml:cn>
</mathml:apply>
</mathml:apply>
<mathml:apply>
<mathml:divide/>
<mathml:apply>
<mathml:power/>
<mathml:ci> y </mathml:ci>
<mathml:cn> 2 </mathml:cn>
</mathml:apply>
<mathml:apply>
<mathml:power/>
<mathml:ci> b </mathml:ci>
<mathml:cn> 2 </mathml:cn>
</mathml:apply>
</mathml:apply>
</mathml:apply>
</mathml:apply>
</mathml:math>
<xhtml:hr/>
<xhtml:p>Last Modified January 10, 2002</xhtml:p>
</xhtml:body>
</xhtml:html>
<!ATTLIST svg:svg xmlns:svg (CDATA)
#FIXED "http://www.w3.org/2000/svg">
<svg:svg width="12cm" height="10cm">
<svg:ellipse rx="110" ry="130" />
<svg:rect x="4cm" y="1cm" width="3cm" height="6cm" />
</svg:svg>
An attribute can be placed in a namespace by prefixing its name.
Normally only used for global atributes that can appear on any element such as XLink attributes rather than attributes that are tied to a specific element.
Unprefixed attributes are never in any namespace.
Being an attribute of an element in the http://www.w3.org/1999/xhtml
namespace is not sufficient to put the attribute in the http://www.w3.org/1999/xhtml
namespace.
The only way an attribute belongs to a namespace is if it has a declared prefix,
like xlink:type
and xlink:href
.
<xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:xlink="http://www.w3.org/1999/xlink">
<xhtml:head><xhtml:title>Three Namespaces</xhtml:title></xhtml:head>
<xhtml:body>
<xhtml:h1 align="center">An Ellipse and a Rectangle</xhtml:h1>
<svg:svg xmlns:svg="http://www.w3.org/2000/svg"
width="12cm" height="10cm">
<svg:ellipse rx="110" ry="130" />
<svg:rect x="4cm" y="1cm" width="3cm" height="6cm" />
</svg:svg>
<xhtml:p xlink:type="simple"
xlink:href="ellipses.html">
More about ellipses
</xhtml:p>
<xhtml:p xlink:type="simple" xlink:href="rectangles.html">
More about rectangles
</xhtml:p>
<xhtml:hr/>
<xhtml:p>Last Modified February 13, 2000</xhtml:p>
</xhtml:body>
</xhtml:html>
Many XML applications have recommended prefixes. For example,
SVG elements often use the prefix svg
and Resource
Description Framework (RDF) elements often have the prefix rdf
.
However, these prefixes are simply conventions, and can be changed based on necessity, convenience or whim.
Before a prefix can be used, it must be bound to a URI.
These URIs are standardized, not the prefixes.
The prefix can change as long as the URI stays the same.
Indicates that an unprefixed element and all its unprefixed descendant
elements belong to a particular namespace by attaching an xmlns
attribute with no prefix:
<DATASCHEMA xmlns="http://www.w3.org/2000/P3Pv1">
<DATA name="vehicle.make" type="text" short="Make"
category="preference" size="31"/>
<DATA name="vehicle.model" type="text" short="Model"
category="preference" size="31"/>
<DATA name="vehicle.year" type="number" short="Year"
category="preference" size="4"/>
<DATA name="vehicle.license.state." type="postal." short="State"
category="preference" size="2"/>
<DATA name="vehicle.license.number" type="text"
short="License Plate Number" category="preference" size="12"/>
</DATASCHEMA>
Both the DATASCHEMA
and DATA
elements are in the
http://www.w3.org/2000/P3Pv1 namespace.
Default namespaces apply only to elements, not to attributes.
The name
, type
,
short
, category
, and size
attributes are not in any namespace.
You can change the default namespace within a particular
element by adding an xmlns
attribute to the element.
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:xlink="http://www.w3.org/1999/xlink">
<head><title>Three Namespaces</title></head>
<body>
<h1 align="center">An Ellipse and a Rectangle</h1>
<svg xmlns="http://www.w3.org/2000/svg"
width="12cm" height="10cm">
<ellipse rx="110" ry="130" />
<rect x="4cm" y="1cm" width="3cm" height="6cm" />
</svg>
<p xlink:type="simple" xlink:href="ellipses.html">
More about ellipses
</p>
<p xlink:type="simple" xlink:href="rectangles.html">
More about rectangles
</p>
<hr/>
<p>Last Modified February 13, 2000</p>
</body>
</html>
<!ATTLIST svg xmlns (CDATA)
#FIXED "http://www.w3.org/2000/svg">
<svg width="12cm" height="10cm">
<ellipse rx="110" ry="130" />
<rect x="4cm" y="1cm" width="3cm" height="6cm" />
</svg>
Purely formal
Can point somewhere but do not have to
These are three different namespaces:
http://www.w3.org/1999/XSL/Transform
http://www.w3.org/1999/XSL/Transform/
http://www.w3.org/1999/XSL/Transform/index.html
DTDs must declare the qualified names
<!ELEMENT svg:text (#PCDATA)>
If the prefix changes, the DTD needs to change too.
Must use double indirection
<!ENTITY % NS.prefixed "IGNORE" >
<!ENTITY % MATHML.prefixed "%NS.prefixed;" >
<!ENTITY % MATHML.xmlns "http://www.w3.org/1998/Math/MathML" >
<!ENTITY % MATHML.prefix "m" >
<![%MATHML.prefixed;[
<!ENTITY % MATHML.xmlns.extra.attrib "" >
]]>
<!ENTITY % MATHML.xmlns.extra.attrib "" >
<![%MATHML.prefixed;[
<!ENTITY % MATHML.pfx "%MATHML.prefix;:" >
<!ENTITY % MATHML.xmlns.attrib
"xmlns:%MATHML.prefix; CDATA #FIXED '%MATHML.xmlns;'
%MATHML.xmlns.extra.attrib;"
>
]]>
<!ENTITY % MATHML.pfx "" >
<!ENTITY % MATHML.xmlns.attrib
"xmlns CDATA #FIXED '%MATHML.xmlns;'
%MATHML.xmlns.extra.attrib;"
>
<![%NS.prefixed;[
<!ENTITY % XHTML.xmlns.extra.attrib
"%MATHML.xmlns.attrib;" >
]]>
<!ENTITY % XHTML.xmlns.extra.attrib ""
>
<!-- Section B: MathML Qualified Names ::::::::::::::::::::::::::::: -->
<!-- 9. This section declares parameter entities used to provide
namespace-qualified names for all MathML element types.
-->
<!ENTITY % abs.qname "%MATHML.pfx;abs" >
<!ENTITY % and.qname "%MATHML.pfx;and" >
...
Only works in external DTD subset
<?xml version="1.0"?> <!DOCTYPE math:math PUBLIC "-//W3C//DTD MathML 2.0//EN" "mathml2.dtd" [ <!ENTITY % NS.prefixed "INCLUDE" > <!ENTITY % MATHML.prefix "math" > ]> <math:math xmlns="http://www.w3.org/1998/Math/MathML"> <math:apply> <math:eq/> <math:cn> 1 </math:cn> <math:apply> <math:plus/> <math:apply> <math:divide/> <math:apply> <math:power/> <math:ci> x </math:ci> <math:cn> 2 </math:cn> </math:apply> <math:apply> <math:power/> <math:ci> a </math:ci> <math:cn> 2 </math:cn> </math:apply> </math:apply> <math:apply> <math:divide/> <math:apply> <math:power/> <math:ci> y </math:ci> <math:cn> 2 </math:cn> </math:apply> <math:apply> <math:power/> <math:ci> b </math:ci> <math:cn> 2 </math:cn> </math:apply> </math:apply> </math:apply> </math:apply> </math:math>
URI matters; prefix doesn't
Do not have to declare xmlns
and xmlns:prefix
attributes
Namespaces are used on elements and attribute values, but not attribute names
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <SONG xmlns="http://ibiblio.org/xml/namespace/song" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.cafeconleche.org/namespace/song namespace_song.xsd" > <TITLE>Hot Cop</TITLE> <PHOTO ALT="Victor Willis in Cop Outfit" WIDTH="100" HEIGHT="200"/> <COMPOSER>Jacques Morali</COMPOSER> <COMPOSER>Henri Belolo</COMPOSER> <COMPOSER>Victor Willis</COMPOSER> <PRODUCER>Jacques Morali</PRODUCER> <PUBLISHER>PolyGram Records</PUBLISHER> <YEAR>1978</YEAR> <ARTIST>Village People</ARTIST> </SONG>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://ibiblio.org/xml/namespace/song" targetNamespace="http://ibiblio.org/xml/namespace/song" elementFormDefault="qualified" attributeFormDefault="unqualified" > <xsd:element name="SONG" type="SongType"/> <xsd:complexType name="SongType"> <xsd:sequence> <xsd:element name="TITLE" type="xsd:string"/> <xsd:element name="PHOTO" type="PhotoType" minOccurs="0"/> <xsd:element name="COMPOSER" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="PRODUCER" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="PUBLISHER" type="xsd:string" minOccurs="0"/> <xsd:element name="YEAR" type="xsd:gYear"/> <xsd:element name="ARTIST" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="PhotoType"> <xsd:complexContent> <xsd:restriction base="xsd:anyType"> <xsd:attribute name="ALT" type="xsd:string"/> <xsd:attribute name="WIDTH" type="xsd:nonNegativeInteger"/> <xsd:attribute name="HEIGHT" type="xsd:nonNegativeInteger"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType> </xsd:schema>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:song="http://www.cafeconleche.org/namespace/song" targetNamespace="http://www.cafeconleche.org/namespace/song" elementFormDefault="qualified" attributeFormDefault="unqualified" > <element name="song:SONG" type="song:SongType"/> <complexType name="song:SongType"> <sequence> <element name="song:TITLE" type="string"/> <element name="song:PHOTO" type="song:PhotoType" minOccurs="0"/> <element name="song:COMPOSER" type="string" maxOccurs="unbounded"/> <element name="song:PRODUCER" type="string" minOccurs="0" maxOccurs="unbounded"/> <element name="song:PUBLISHER" type="string" minOccurs="0"/> <element name="song:YEAR" type="gYear"/> <element name="song:ARTIST" type="string" minOccurs="0" maxOccurs="unbounded"/> </sequence> </complexType> <complexType name="song:PhotoType"> <complexContent> <restriction base="anyType"> <attribute name="ALT" type="string"/> <attribute name="WIDTH" type="nonNegativeInteger"/> <attribute name="HEIGHT" type="nonNegativeInteger"/> </restriction> </complexContent> </complexType> </schema>
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <SONG xmlns="http://www.cafeconleche.org/namespace/song" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.cafeconleche.org/namespace/song xlink_song.xsd http://www.w3.org/1999/xlink xlink.xsd" > <TITLE>Hot Cop</TITLE> <PHOTO xlink:type="simple" xlink:href="hotcop.jpg" xlink:actuate="onLoad" xlink:show="embed" ALT="Victor Willis in Cop Outfit" WIDTH="100" HEIGHT="200"/> <COMPOSER>Jacques Morali</COMPOSER> <COMPOSER>Henri Belolo</COMPOSER> <COMPOSER>Victor Willis</COMPOSER> <PRODUCER>Jacques Morali</PRODUCER> <PUBLISHER>PolyGram Records</PUBLISHER> <YEAR>1978</YEAR> <ARTIST>Village People</ARTIST> </SONG>
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3.org/1999/xlink" targetNamespace="http://www.w3.org/1999/xlink" > <xsd:attribute name="type" type="xsd:string" fixed="simple" /> <xsd:attribute name="href" type="xsd:anyURI" /> <xsd:attribute name="actuate" type="xsd:string" fixed="onLoad" /> <xsd:attribute name="show" type="xsd:string" fixed="embed" /> </xsd:schema>
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.cafeconleche.org/namespace/song" xmlns:xlink="http://www.w3.org/1999/xlink" targetNamespace="http://www.cafeconleche.org/namespace/song" elementFormDefault="qualified" attributeFormDefault="unqualified" > <xsd:import namespace="http://www.w3.org/1999/xlink" schemaLocation="xlink.xsd"/> <xsd:element name="SONG" type="SongType"/> <xsd:complexType name="PhotoType"> <xsd:attribute name="WIDTH" type="xsd:positiveInteger" use="required" /> <xsd:attribute name="HEIGHT" type="xsd:positiveInteger" use="required" /> <xsd:attribute name="ALT" type="xsd:string" use="required" /> <xsd:attribute ref="xlink:type"/> <xsd:attribute ref="xlink:href" use="required"/> <xsd:attribute ref="xlink:actuate"/> <xsd:attribute ref="xlink:show"/> </xsd:complexType> <xsd:complexType name="SongType"> <xsd:sequence> <xsd:element name="TITLE" type="xsd:string"/> <xsd:element name="PHOTO" type="PhotoType"/> <xsd:element name="COMPOSER" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="PRODUCER" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="PUBLISHER" type="xsd:string" minOccurs="0"/> <xsd:element name="YEAR" type="xsd:gYear"/> <xsd:element name="ARTIST" type="xsd:string" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
Specifies whether locally declared elements and attributes are namespace qualified
In a schema, a locally declared element is an element
declared directly inside a complexType
(not by reference)
<complexType name="CarType">
<sequence>
<element name="steering_wheel" type="SteeringWheelType"/>
<element name="wheel" type="WheelType"/>
</sequence>
</complexType>
In an instance document, this is a locally declared element:
<vg:car xmlns:vg="http://www.vg.org/autos/"
width="12cm" height="10cm">
<steering_wheel diameter="24cm" />
<wheel number="4" />
</vg:car>
Does not apply to globally declared items
Default is unqualified for both elements and attributes. This is correct for attributes but backwards from what it should be for elements.
Always set elementFormDefault
to
qualified
and don't use local elements.
Namespaces were added to XML 1.0 after the fact, but care was taken to ensure backwards compatibility.
An XML 1.0 parser that does not know about namespaces will most likely not have any troubles reading a document that uses namespaces.
A namespace aware parser also checks to see that all prefixes are mapped to URIs. Otherwise it behaves almost exactly like a non-namespace aware parser.
Other software that sits on top of the raw XML parser, an XSLT engine for example, may treat elements differently depending on what namespace they belong to. However, the XML parser itself mostly doesn't care as long as all well-formedness and namespace constraints are met.
A possible exception occurs in the unlikely event that elements with different prefixes belong to the same namespace or elements with the same prefix belong to different namespaces
Many parsers have the option of whether to report namespace violations so that you can turn namespace processing on or off as you see fit.
SAX2 is fully namespace aware:
public void startElement(String namespaceURI, String localName, String qualifiedName, Attributes atts)
throws SAXException
public void endElement(String namespaceURI, String localName, String qualifiedName)
throws SAXException
public void startPrefixMapping(String prefix, String uri)
throws SAXException
public void endPrefixMapping(String prefix)
throws SAXException
Namespace handling is controlled by these two properties:
http://xml.org/sax/features/namespaces
http://xml.org/sax/features/namespace-prefixes
http://xml.org/sax/features/namespaces feature determines
whether namespace URIs and local names are passed to
startElement()
and endElement()
.
The default, true, passes both namespace URIs and local names.
However, if http://xml.org/sax/features/namespaces is false,
then the parser may pass the namespace URI and the local name,
or it may just pass empty strings for these two arguments.
http://xml.org/sax/features/namespace-prefixes feature determines two things:
Whether or not namespace declaration xmlns
and
xmlns:prefix
attributes are included in the Attributes
object passed to startElement()
.
The default, false, is not to include them.
Whether or not the qualified names should be passed as the
third argument to the startElement()
method.
The default, false, is, not to require qualified names.
However, even if http://xml.org/sax/features/namespace-prefixes is false,
parsers are allowed to report the qualified name, and most do so.
In other words,
The parser is only guaranteed to provide the namespace URIs and local names of elements and attributes if http://xml.org/sax/features/namespaces is true (which it is by default).
The parser is only guaranteed to provide the qualified names of elements and attributes if http://xml.org/sax/features/namespace-prefixes is true (which it is not by default).
The parser provides namespace declaration attributes if and only if http://xml.org/sax/features/namespace-prefixes is true (which it is not by default).
The parser always has the option to provide the namespace URI, local name, and qualified name, regardless of the values of http://xml.org/sax/features/namespaces and http://xml.org/sax/features/namespace-prefixes. However, you should not rely on this behavior.
Bottom line: the defaults are fine as long as you don’t care about namespace prefixes, only local names and URIs.
By default namespace declaration attributes such as
xmlns="http://ns.cafeconleche.org/Orders/"
and
xmlns:xlink="http://www.w3.org/1999/xlink"
are not included in the list of
attributes passed to startElement()
.
Instead each such namespace declaration attribute is
signaled
by a call to startPrefixMapping()
immediately before the startElement()
call
corresponding to the start tag of the element where the declaration
appears. Furthermore, the endElement()
call corresponding to the end tag of that element is immediately followed
by an endPrefixMapping()
call.
public void startPrefixMapping(String prefix, String uri) throws SAXException
public void endPrefixMapping(String prefix) throws SAXException
Most of the time you can ignore these events. As long as you only care about
URIs and prefixes on element and attribute names, then
there’s really no need to pay attention to these methods.
You simply inspect
the URIs and prefixes passed in the arguments to the
startElement()
method.
However, in a few applications, including XSLT and schemas, prefixes appear in attribute values and even element content. If so, you need to keep track of which URIs particular prefixes are bound to where.
Keep the prefixes and URIs on a per-document
stack. startPrefixMapping()
pushes the pair onto the stack.
endPrefixMapping()
pops them off.
If at any time while parsing you need to know
the current binding of a prefix you’ve encountered, simply
search down the stack starting with the top. The first
binding you encounter for the prefix is the one you want.
org.xml.sax.helpers.NamespaceSupport
implements this logic.
package org.xml.sax.helpers;
public class NamespaceSupport {
public final static String XMLNS
= "http://www.w3.org/XML/1998/namespace";
public NamespaceSupport();
public void reset();
public void pushContext();
public void popContext();
public boolean declarePrefix(String prefix, String uri);
public String[] processName(String qualifedName,
String parts[], boolean isAttribute);
public String getURI(String prefix);
public Enumeration getPrefixes();
public String getPrefix(String uri);
public Enumeration getPrefixes(String uri);
public Enumeration getDeclaredPrefixes();
}
Call
pushContext()
in the first
startPrefixMapping()
event
before each startElement()
,
declarePrefix()
every
startPrefixMapping()
event,
and
popContext()
in the first
endPrefixMapping()
event
after each endElement()
event.
Add namespace declarations to a context by passing their
prefix and URI as strings to
declarePrefix()
inside each
startPrefixMapping()
call.
Use the empty string
as the prefix to declare the default namespace:
private NamespaceSupport namespaces;
private boolean needNewContext = true;
public void startDocument() {
namespaces = new NamespaceSupport();
}
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
if (needNewContext) {
namespaces.pushContext();
needNewContext = false;
}
namespaces.declarePrefix(prefix, uri);
}
public void endPrefixMapping(String prefix)
throws SAXException {
if (!needNewContext) {
namespaces.popContext();
needNewContext = true;
}
}
The getDeclaredPrefixes()
,
getPrefix()
,
getPrefixes()
,
and getURI()
methods all return
various information about the namespaces in scope in the
current context. For example, this startElement()
method
prints the namespace URI for the values of
type attributes, such as might be found in a schema:
public void startElement(String namespaceURI, String localName,
String qualifiedName, Attributes atts) throws SAXException {
String value = atts.getValue("type");
if (value != null) {
String prefix = "";
if (value.indexOf(':') >= 0) {
prefix = value.substring(0, value.indexOf(':'));
}
String uri = namespaces.getURI(prefix);
}
}
To reuse a
NamespaceSupport
for a new document,
call reset()
, probably from the
startDocument()
method.
DOM Level 2 methods are namespace aware:
public Element createElementNS(String namespaceURI, String qualifiedName)
throws DOMException
public Attr createAttributeNS(String namespaceURI, String qualifiedName)
throws DOMException
public NodeList getElementsByTagNameNS(String namespaceURI, String localName)
These set all four properties: node name, local name, namespace
URI, and prefix.
DOM Level 1 Methods are not namespace aware:
public Element createElement(String tagName) throws DOMException;
public Attr createAttribute(String name) throws DOMException;
public NodeList getElementsByTagName(String tagname);
These set the node name only. The local name, namespace
URI, and prefix are all null.
Unlike SAX, DOM does include namespace declaration attributes
such as
xmlns="http://ns.cafeconleche.org/Orders/"
and
xmlns:xlink="http://www.w3.org/1999/xlink"
in lists of attributes.
Provide a namespace URI and full prefixed name when creating elements and attributes in a namespace
Use local name and URI when searching for an element or attribute
Serialization is tricky, and not all implementations get the namespace declarations in the right place.
JDOM is fully namespace aware
A namespace URL (which may be the empty string) is attached to each element and attribute.
Additional namespace declarations (beyond the element's own namespace) can be attached to any element:
public void addNamespaceDeclaration(Namespace additionalNamespace)
If you need to know what URI is mapped to a given prefix or what the default namespace is, you ask the nearest ancestor-or-self element:
public Namespace getNamespace(String prefix)
The Namespace
class uses the flyweight design pattern
so that multiple elements in the same namespace don't eat huge amounts of memory.
Serialization puts namespace declarations in the right places.
The flaw is the conflation of name, location and identity but that flaw is the basic feature by which the WWW runs so we are stuck there. All the handwaving about URN/URI/URL doesn't avoid the simple fact that if one puts http:// anywhere in browser display space, the system colors it blue and puts up a finger.
The monkey expects a resource and when it doesn't get one, it shocks the monkey. Monkeys don't read specs to find out why they should be shocked. They turn red and put up a finger.
--Claude L. Bullard on the xml-dev mailing list
An XML application for pages at the end of a namespace URI.
Invented by Jonathan Borden, Tim Bray, and others on the xml-dev mailing list
A Resource Directory provides a text description of some class of resources and of other resources related to that class. It also contains a directory of links to these related resources.
An XML namespace is one possible kind of resource. Related resources might include schemas, stylesheets, Java content handlers, browser plug-ins, and more.
The RDDL DTD is an extension of XHTML Basic 1.0 using XHTML Modularization.
RDDL adds one new element to XHTML Basic 1.0: resource
.
The resource element is in
the http://www.rddl.org/
namespace.
The rddl
prefix is customary.
The rddl:resource
element is a simple XLink;
that is, it has an xlink:type="simple"
attribute
The rddl:resource
element
can be placed anywhere in HTML
where a p
element may appear.
The rddl:resource
can contain may of the XHTML Basic elements
(headings, paragraphs, lists, hyperlinks,
forms, tables, images, meta information )
that the body
element may contain.
The content of the
rddl:resource
element should describe the associated resource.
Resource elements can contain other resource elements.
For the http://www.cafeconleche.org/baseball/ namespace:
<rddl:resource xlink:href="baseball.css"
xlink:role="http://www.rddl.org/arcrole.htm#CSS">
<div id="CSS" class="resource">
<h3>CSS Stylesheet</h3>
<p>A <a href="baseball.css">CSS stylesheet</a>
for baseball statistics documents.</p>
</div>
</rddl:resource>
<rddl:resource xlink:href="baseball.dtd"
xlink:role="http://www.rddl.org/arcrole.htm#DTD">
<div id="DTD" class="resource">
<h3>DTD</h3>
<p>A <a href="baseball.dtd">DTD</a> for ,
baseball statistics</p>
</div>
</rddl:resource>
<!DOCTYPE html PUBLIC "-//XML-DEV//DTD XHTML RDDL 1.0//EN" "http://www.openhealth.org/RDDL/rddl-xhtml.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:rddl="http://www.rddl.org/"> <head> <title>RDDL Resources for Baseball Statistics</title> </head> <body> <h1>RDDL Resources for Baseball Statistics</h1> <p>This is a sample RDDL document used in Elliotte Rusty Harold's seminars including</p> <ul> <li>XLinks and XPointers</li> <li>Namespaces in XML</li> </ul> <p>It describes resources related to the baseball statistics DTD developed in the XML Bible and located at the namespace <code>http://www.cafeconleche.org/baseball/</code>.</p> <p> <img src="../smallbiblecover.jpg" width="127" height='156' alt="Cover of the XMl Bible"/> </p> <rddl:resource xlink:href="baseball.css" xlink:role="http://www.rddl.org/arcrole.htm#CSS"> <div id="CSS" class="resource"> <h3>CSS Stylesheet</h3> <p>A <a href="baseball.css">CSS stylesheet</a> for baseball statistics documents.</p> </div> </rddl:resource> <rddl:resource xlink:href="baseball.dtd" xlink:role="http://www.rddl.org/arcrole.htm#DTD"> <div id="DTD" class="resource"> <h3>DTD</h3> <p>A <a href="baseball.dtd">DTD</a> for , baseball statistics</p> </div> </rddl:resource> <p> Copyright 2001 <a href="http://www.macfaq.com/personal.html">Elliotte Rusty Harold</a><br class="empty"/> <a href="mailto:elharo@metalab.unc.edu">elharo@metalab.unc.edu</a><br class="empty"/> Last Modified Sunday, January 14, 2001 </p> </body> </html>View in Browser
Contains a URI, possibly relative, pointing to the related resource
Identifies the type of the related resource with an absolute URI
Can be the namespace URI of the root element:
http://www.w3.org/2000/10/XMLSchema
http://www.rddl.org/arcrole.htm#SOCAT
http://www.ascc.net/xml/schematron
http://www.xml.gr.jp/xmlns/relaxCore
http://www.w3.org/2000/01/rdf-schema#
Can be a MIME media type URI at http://www.isi.edu/in-notes/iana/assignments/media-types/
http://www.isi.edu/in-notes/iana/assignments/media-types/application/pdf
Can be a spec-defined URI:
http://www.rddl.org/natures#SOCAT
For situations where the nature/role alone isn't enough, this offers an additional URI defining the purpose of the link.
Well-known purposes include:
http://www.rddl.org/purposes#validation
http://www.rddl.org/purposes#schema-validation
http://www.rddl.org/purposes#module
http://www.rddl.org/purposes#schema-module
http://www.rddl.org/purposes#entities
http://www.rddl.org/purposes#notations
http://www.rddl.org/purposes/software#xslt-extension
http://www.rddl.org/purposes/software#software-package
http://www.rddl.org/purposes/software#software-project
http://www.rddl.org/purposes#JAR
http://www.rddl.org/purposes/software#reference
http://www.rddl.org/purposes/software#normative-reference
http://www.rddl.org/purposes/software#non-normative-reference
http://www.rddl.org/purposes#prior-version
http://www.rddl.org/purposes#definition
http://www.rddl.org/purposes#icon
http://www.rddl.org/purposes#directory
http://www.rddl.org/purposes#alternate
May contain a short, human readable description of the resource as plain, unmarked text.
Should not be used as a substitute for a more complete,
HTML-marked-up description of the related resource in the
contents of the rddl:resource
element.
These two attributes are not used on
rddl:resource
. They must
have the value none
.
<!ELEMENT rddl:resource (#PCDATA | %Flow.mix;)*>
<!ATTLIST rddl:resource
id ID #IMPLIED
xlink:type (simple) #FIXED "simple"
xmlns:rddl CDATA #FIXED 'http://www.rddl.org/'
xml:lang NMTOKEN #IMPLIED
xlink:arcrole CDATA #IMPLIED
xlink:href CDATA #IMPLIED
xlink:role CDATA 'http://www.rddl.org/#resource'
xlink:title CDATA #IMPLIED
xlink:show (none) #FIXED "none"
xlink:actuate (none) #FIXED "none"
>
The RDDL Specification: http://www.rddl.org/ (Written in RDDL)
This presentation: http://www.cafeconleche.org/slides/xmlone/london2002/namespaces/
XML in a Nutshell
Elliotte Rusty Harold and W. Scott Means
O'Reilly & Associates, 2001
ISBN 0-596-00058-8
XML Bible, Gold edition
Elliotte Rusty Harold
Hungry Minds, 2001
ISBN 0-7645-4819-0
Chapter 13: Namespaces
Chapter 28: RDDL
Who's Jon Johansen? http://www.eff.org/IP/DeCSS_prosecutions/Johansen_DeCSS_case/