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 in a qualified name is associated with a URI.
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 can recognize 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:mrow>
<mathml:mi>f(0)</mathml:mi>
<mathml:mo>=</mathml:mo>
<mathml:mn>0</mathml:mn>
</mathml:mrow>
</mathml:math>
<xhtml:hr/>
<xhtml:p>Last Modified February 13, 2000</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.
Thus in the above example 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
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.
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 % mathml-colon ''>
<!ENTITY % mathml-prefix ''>
<!ENTITY % mathml-exp '%mathml-prefix;%mathml-colon;exp' >
<!ENTITY % mathml-abs '%mathml-prefix;%mathml-colon;abs' >
<!ENTITY % mathml-arg '%mathml-prefix;%mathml-colon;arg' >
<!ENTITY % mathml-real '%mathml-prefix;%mathml-colon;real' >
<!ENTITY % mathml-imaginary '%mathml-prefix;%mathml-colon;imaginary' >
<!ELEMENT %mathml-real; (%mathml-exp; | %mathml-abs;)*>
Only works in external DTD subset
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://ibiblio.org/xml/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://ibiblio.org/xml/namespace/song" targetNamespace="http://ibiblio.org/xml/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://ibiblio.org/xml/namespace/song" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://ibiblio.org/xml/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://ibiblio.org/xml/namespace/song" xmlns:xlink="http://www.w3.org/1999/xlink" targetNamespace="http://ibiblio.org/xml/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.
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/arcrole.htm#DTD
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:embed (none) #FIXED "none"
xlink:actuate (none) #FIXED "none"
>
The RDDL Specification: http://www.rddl.org/ (Written in RDDL)
This presentation: http://www.ibiblio.org/xml/slides/sd2001west/namespaces
XML in a Nutshell
Elliotte Rusty Harold and W. Scott Means
O'Reilly & Associates, 2001
ISBN 0-596-00058-8
XML Bible, second edition
Elliotte Rusty Harold
Hungry Minds, 2001
ISBN 0-7645-4760-7