Namespaces


Namespaces

Elliotte Rusty Harold

XML & Web Services 2002 London

Tuesday, March 12, 2002

elharo@metalab.unc.edu

http://www.cafeconleche.org/


Raison d'etre

  1. To distinguish between elements and attributes from different vocabularies with different meanings.

  2. To group all related elements and attributes together so that a parser can easily recognize them.


The Need for Namespaces


Namespaces disambiguate elements


URIs


Namespace Syntax


Namespace URIs


Binding Prefixes to Namespace URIs


Binding Prefixes to Namespace URIs Example

<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>

Binding namespaces to prefixes with #FIXED ATTLIST declarations

<!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>

Namespaces and Attributes

<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>

URIs matter, not prefixes


The Default Namespace


Multiple default namespaces


Binding default namespaces to elements with #FIXED ATTLISTs

<!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>

Namespace URIs do not necessarily point to a document, page, or schema


Parsers compare namespace URIs character by character

These are three different namespaces:


Namespaces and DTDs


Parameter Entity References for Namespace Prefixes


Overriding the prefix in the internal 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>


Namespaces and Schemas


A Song with a Namespace

<?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>

A Schema for a Document that Uses the Default Namespace

<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>

Reversing the Default Namespace

<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>

Multiple Namespaces, Multiple Schemas

<?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>

XLink Schema

<?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>

Song Schema with XLink Support

<?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>

elementFormDefault and attributeFormDefault


How Parsers Handle Namespaces


Namespaces in SAX


Prefix Mappings


Namespaces in DOM


Namespaces in JDOM


What Goes at the end of a Namespace URI?

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


The Resource Directory Description Language


The resource Element


Example RDDL 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>

An Example RDDL Document

<!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

The xlink:href attribute


The xlink:role attribute


The xlink:arcrole attribute


The xlink:title attribute


The xlink:show and xlink:actuate attributes


Declarations for rddl:resource

<!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"
>

To Learn More


To Learn More


Index | Cafe con Leche

Copyright 2001, 2002 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified March 21, 2002