XML namespace: Difference between revisions

The educational technology and digital learning wiki
Jump to navigation Jump to search
mNo edit summary
Line 1: Line 1:
{{Incomplete}}
{{Incomplete}}
This is a draft for a tutorial article, but it needs to be fixed in several places.


This is an ''introductory'' XML namespace tutorial.
Note on its production: It's a Framemaker to Word to Mediawiki translation from parts of teaching slides - 21:50, 20 March 2007 (MET)
 
Note on its production: It's a Framemaker to Word to Mediawiki translation from parts of teaching slides. There are some details that need to be fixed - (20 March 2007)


== Definition ==
== Definition ==
Line 33: Line 32:
* http://www.w3.org/2004/CDF/
* http://www.w3.org/2004/CDF/


See also: The [[standard]]s overview article. Most XML-based standards in education (e.g. [[IMS Content Packaging]] make heavy use of namespaces ! In addition, most [[IMS]] and [[SCORM]] standards use [[XML Schema]]s which are associated to XML contents via namespace declarations.
See also: The [[standard]]s overview article. Most XML-based standards in education (e.g. [[IMS Content Packaging]] make heavy use of namespaces ! In addition most [[IMS]] and [[SCORM]] standards use [[XML Schema]] which are associated to XML contents via namespace declarations.


== Namespaces ==
== Namespaces ==
Line 39: Line 38:
=== An example demonstrating the need for namespaces ===
=== An example demonstrating the need for namespaces ===


Here is an XML fragment using '''title''' within a '''bibliography''' element:
Here is an XML fragment using title within a bibliography element:


  <nowiki>book</nowiki>
  <nowiki><</nowiki>book<nowiki>></nowiki>
   <nowiki>title</nowiki>A true story<nowiki>/title</nowiki>
   <nowiki><</nowiki>title<nowiki>></nowiki>A true story<nowiki><</nowiki>/title<nowiki>></nowiki>
   <nowiki>description</nowiki>A real cool publication<nowiki>/description</nowiki>
   <nowiki><</nowiki>description<nowiki>></nowiki>A real cool publication<nowiki><</nowiki>/description<nowiki>></nowiki>
  <nowiki>/book</nowiki>
  <nowiki><</nowiki>/book<nowiki>></nowiki>


A 2nd XML fragment uses '''title''' in a '''employees''' record:
A 2nd XML fragment uses title in a employees record:
  <nowiki>record</nowiki>
  <nowiki><</nowiki>record<nowiki>></nowiki>
   <nowiki>name</nowiki>Miller<nowiki>/name</nowiki>  <nowiki>title</nowiki>Dr. <nowiki>/title</nowiki>
   <nowiki><</nowiki>name<nowiki>></nowiki>Miller<nowiki><</nowiki>/name<nowiki>></nowiki>  <nowiki><</nowiki>title<nowiki>></nowiki>Dr. <nowiki><</nowiki>/title<nowiki>></nowiki>
   <nowiki>publications</nowiki> ...  <nowiki>/publications</nowiki>
   <nowiki><</nowiki>publications<nowiki>></nowiki> ...  <nowiki><</nowiki>/publications<nowiki>></nowiki>
  <nowiki>/record</nowiki>
  <nowiki><</nowiki>/record<nowiki>></nowiki>


If these two XML fragments were added together, there would be a name conflict because both contain a <nowiki>title</nowiki> element with different meaning. In one case we mean ''book title'' and in the other ''something link nothing, PhD, etc.''. E.g. a stylesheet would have to render these elements very differently. But we can solve this problem by adding prefixes, like this:
If these two XML fragments were added together, there would be a name conflict because both contain a <nowiki><</nowiki>title<nowiki>></nowiki> element with different meaning. But we can solve this like this:


  <nowiki>''employees'':record</nowiki>
  <nowiki><</nowiki>''employees'':record<nowiki>></nowiki>
  <nowiki>''employees'':name</nowiki>Miller<nowiki>/''employees'':name</nowiki>
  <nowiki><</nowiki>''employees'':name<nowiki>></nowiki>Miller<nowiki><</nowiki>/''employees'':name<nowiki>></nowiki>
  <nowiki>''employees'':title</nowiki>Dr.  <nowiki>/''employees'':title</nowiki>
  <nowiki><</nowiki>''employees'':title<nowiki>></nowiki>Dr.  <nowiki><</nowiki>/''employees'':title<nowiki>></nowiki>
  <nowiki>''employees'':publications</nowiki>
  <nowiki><</nowiki>''employees'':publications<nowiki>></nowiki>
   <nowiki>''biblio'':book</nowiki>
   <nowiki><</nowiki>''biblio'':book<nowiki>></nowiki>
   <nowiki>''biblio'':title</nowiki>A true story<nowiki>/biblio:title</nowiki>
   <nowiki><</nowiki>''biblio'':title<nowiki>></nowiki>A true story<nowiki><</nowiki>/biblio:title<nowiki>></nowiki>
   <nowiki>''biblio'':description</nowiki>A real cool publication<nowiki>/biblio:description</nowiki>
   <nowiki><</nowiki>''biblio'':description<nowiki>></nowiki>A real cool publication<nowiki><</nowiki>/biblio:description<nowiki>></nowiki>
   <nowiki>/''biblio'':book</nowiki>
   <nowiki><</nowiki>/''biblio'':book<nowiki>></nowiki>
  <nowiki>/''employees'':publications</nowiki>
  <nowiki><</nowiki>/''employees'':publications<nowiki>></nowiki>
  <nowiki>/''employees'':record</nowiki>
  <nowiki><</nowiki>/''employees'':record<nowiki>></nowiki>


=== Declaring namespaces ===
=== Declaring namespaces ===
Line 72: Line 71:


# Create or identify a namespace identifier you wish to use: An XML namespace is identified by a unique URI reference, usually a URL.
# Create or identify a namespace identifier you wish to use: An XML namespace is identified by a unique URI reference, usually a URL.
# The URL does '''need not point to anything on the Internet'''. It is just used as a unique string, i.e. a name !
# The URL '''need not point to anything on the Internet'''. It is just used as a unique string, i.e. a name  
# Therefore if some namespace URLs below are broken there is absolutly nothing to worry about. However, most namespace identifiers actually point to a real web page that either provides an explanation or at least informations about the organization/author that created the namespace. This means that if you plan to create your own namespaces, you should use the name of any webpage over which you have control.
# Therefore if some namespace URLs below are broken there is absolutly nothing to worry about. However, most namespace identifiers actually point to a real web page that either provides an explanation or at least informations about the organization. This means that if you plan to create your own namespaces, you should use the name of any webpage over which you have control.
# Make a namespace declaration within the element that belongs to this namespace, i.e. map a prefix of your choice to a unique URI.
# Make a namespace declaration within the element that belongs to this namespace, i.e. map a prefix of your choice to a unique URI.


Line 80: Line 79:
(1) declaring a namespace that will require insertion of prefixes
(1) declaring a namespace that will require insertion of prefixes


  <nowiki>prefix:element xmlns:prefix="URI"</nowiki>
  <nowiki><</nowiki>prefix:element xmlns:prefix="URI"<nowiki>></nowiki>
e.g.
e.g.
  <nowiki>html:html xmlns:html=<nowiki>’</nowiki>http://www.w3.org/1999/xhtml<nowiki>’></nowiki>   
  <nowiki><</nowiki>html:html xmlns:html=<nowiki>’</nowiki>http://www.w3.org/1999/xhtml<nowiki>’></nowiki>   
(2) declaring a default namespace for an element plus its children (by default they belong to namespace of the parent). See also below.
(2) declaring a default namespace (element <nowiki>+</nowiki> children belong to this namespace by default)


  <nowiki>element xmlns="URI"</nowiki>
  <nowiki><</nowiki>element xmlns="URI"<nowiki>></nowiki>
e.g.
e.g.
  <nowiki>html xmlns=<nowiki>’</nowiki>http://www.w3.org/1999/xhtml<nowiki>’></nowiki>  .....
  <nowiki><</nowiki>html xmlns=<nowiki>’</nowiki>http://www.w3.org/1999/xhtml<nowiki>’></nowiki>  .....


=== Scoping ===
=== Scoping ===
Line 97: Line 96:
;XHMTL fragment example:
;XHMTL fragment example:


  <nowiki>?xml version="1.0"?</nowiki>
  <nowiki><</nowiki>?xml version="1.0"?<nowiki>></nowiki>
  <nowiki>html:html xmlns:html=<nowiki>’</nowiki>http://www.w3.org/1999/xhtml<nowiki>’></nowiki>
  <nowiki><</nowiki>html:html xmlns:html=<nowiki>’</nowiki>http://www.w3.org/1999/xhtml<nowiki>’></nowiki>
   <nowiki>html:headhtml:title</nowiki>Frobnostication<nowiki>/html:title/html:head</nowiki>
   <nowiki><</nowiki>html:head<nowiki>><</nowiki>html:title<nowiki>></nowiki>Frobnostication<nowiki><</nowiki>/html:title<nowiki>><</nowiki>/html:head<nowiki>></nowiki>
   <nowiki>html:bodyhtml:p</nowiki>Moved to
   <nowiki><</nowiki>html:body<nowiki>><</nowiki>html:p<nowiki>></nowiki>Moved to
   <nowiki>html:a href=<nowiki>’</nowiki>http://frob.example.com<nowiki>’></nowiki>here.<nowiki>/html:a/html:p</nowiki>
   <nowiki><</nowiki>html:a href=<nowiki>’</nowiki>http://frob.example.com<nowiki>’></nowiki>here.<nowiki><</nowiki>/html:a<nowiki>><</nowiki>/html:p<nowiki>></nowiki>
   <nowiki>/html:body</nowiki>
   <nowiki><</nowiki>/html:body<nowiki>></nowiki>
  <nowiki>/html:html</nowiki>
  <nowiki><</nowiki>/html:html<nowiki>></nowiki>


;Example with 2 namespaces (both use URNs instead of URLs)
;Example with 2 namespaces (both use URNs instead of URLs)


  <nowiki>?xml version="1.0"?</nowiki>
  <nowiki><</nowiki>?xml version="1.0"?<nowiki>></nowiki>
  <nowiki>!-- both namespace prefixes are available throughout --</nowiki>
  <nowiki><</nowiki>!-- both namespace prefixes are available throughout --<nowiki>></nowiki>
  <nowiki>bk:book xmlns:bk=<nowiki>’</nowiki>urn:loc.gov:books<nowiki>’</nowiki> xmlns:isbn=<nowiki>’</nowiki>urn:ISBN:0-395-36341-6<nowiki>’></nowiki>
  <nowiki><</nowiki>bk:book xmlns:bk=<nowiki>’</nowiki>urn:loc.gov:books<nowiki>’</nowiki> xmlns:isbn=<nowiki>’</nowiki>urn:ISBN:0-395-36341-6<nowiki>’></nowiki>
   <nowiki>bk:title</nowiki>Cheaper by the Dozen<nowiki>/bk:title</nowiki>
   <nowiki><</nowiki>bk:title<nowiki>></nowiki>Cheaper by the Dozen<nowiki><</nowiki>/bk:title<nowiki>></nowiki>
   <nowiki>isbn:number</nowiki>1568491379<nowiki>/isbn:number</nowiki>
   <nowiki><</nowiki>isbn:number<nowiki>></nowiki>1568491379<nowiki><</nowiki>/isbn:number<nowiki>></nowiki>
  <nowiki>/bk:book</nowiki>
  <nowiki><</nowiki>/bk:book<nowiki>></nowiki>


=== Default namespaces ===
=== Default namespaces ===


If most elements in an XML document belonged to the same namespace, it would be ugly to prefix each element name. Instead, define a default namespace that applies to all non-prefixed elements and attributes.
If most elements in an XML document belonged to the same namespace, it would be ugly to prefix each element name. Instead define a default namespace that applies to all non-prefixed elements and attributes.


  <nowiki>element xmlns="URI"</nowiki> ....
  <nowiki><</nowiki>element xmlns="URI"<nowiki>></nowiki> ....


The default namespace applies to the element in which it was defined and all descendants of that element. But if a descendants has another default namespace defined on it, this new namespace definition overrides the previous one and becomes the default for that element and its descendants.
The default namespace applies to the element on which it was defined and all descendants of that element. But if a descendants has another default namespace defined on it, this new namespace definition overrides the previous one and becomes the default for that element and its descendants.
Note that namespaces does not apply to attribute names ! This is a likely source of trouble for XSLT ...
Note that namespaces does not apply to attribute names ! This is a likely source of trouble for XSLT ...


;XHTML default namespace example:
;XHTML default namespace example:


  <nowiki>?xml version="1.0"?</nowiki>
  <nowiki><</nowiki>?xml version="1.0"?<nowiki>></nowiki>
  <nowiki>!-- elements are in the HTML namespace by default --</nowiki>
  <nowiki><</nowiki>!-- elements are in the HTML namespace by default --<nowiki>></nowiki>
   <nowiki>html xmlns=<nowiki>’</nowiki>http://www.w3.org/1999/xhtml<nowiki>’></nowiki>
   <nowiki><</nowiki>html xmlns=<nowiki>’</nowiki>http://www.w3.org/1999/xhtml<nowiki>’></nowiki>
   <nowiki>headtitle</nowiki>Frobnostication<nowiki>/title/head</nowiki>
   <nowiki><</nowiki>head<nowiki>><</nowiki>title<nowiki>></nowiki>Frobnostication<nowiki><</nowiki>/title<nowiki>><</nowiki>/head<nowiki>></nowiki>
   <nowiki>bodyp</nowiki>Moved to
   <nowiki><</nowiki>body<nowiki>><</nowiki>p<nowiki>></nowiki>Moved to
   <nowiki>a href=<nowiki>’</nowiki>http://frob.example.com<nowiki>’></nowiki>here<nowiki>/a</nowiki>.<nowiki>/p/body</nowiki>
   <nowiki><</nowiki>a href=<nowiki>’</nowiki>http://frob.example.com<nowiki>’></nowiki>here<nowiki><</nowiki>/a<nowiki>></nowiki>.<nowiki><</nowiki>/p<nowiki>><</nowiki>/body<nowiki>></nowiki>
  <nowiki>/html</nowiki>
  <nowiki><</nowiki>/html<nowiki>></nowiki>


Default namespace for a book vocabulary plus a namespace for ISBN example:
Default namespace for a book vocabulary plus a namespace for ISBN example:
  <nowiki>?xml version="1.0"?</nowiki>
  <nowiki><</nowiki>?xml version="1.0"?<nowiki>></nowiki>
  <nowiki>!-- unprefixed element types are from "books" --</nowiki>
  <nowiki><</nowiki>!-- unprefixed element types are from "books" --<nowiki>></nowiki>
   <nowiki>book  xmlns=<nowiki>’</nowiki>urn:loc.gov:books<nowiki>’</nowiki>
   <nowiki><</nowiki>book  xmlns=<nowiki>’</nowiki>urn:loc.gov:books<nowiki>’</nowiki>
  xmlns:isbn=<nowiki>’</nowiki>urn:ISBN:0-395-36341-6<nowiki>’></nowiki>
  xmlns:isbn=<nowiki>’</nowiki>urn:ISBN:0-395-36341-6<nowiki>’></nowiki>
     <nowiki>title</nowiki>Cheaper by the Dozen<nowiki>/title</nowiki>
     <nowiki><</nowiki>title<nowiki>></nowiki>Cheaper by the Dozen<nowiki><</nowiki>/title<nowiki>></nowiki>
     <nowiki>isbn:number</nowiki>1568491379<nowiki>/isbn:number</nowiki>
     <nowiki><</nowiki>isbn:number<nowiki>></nowiki>1568491379<nowiki><</nowiki>/isbn:number<nowiki>></nowiki>
   <nowiki>/book</nowiki>
   <nowiki><</nowiki>/book<nowiki>></nowiki>


;A larger example of namespace scoping
;A larger example of namespace scoping
Line 146: Line 145:
It includes some XHTML whose namespace is declared within the "p" element.
It includes some XHTML whose namespace is declared within the "p" element.


  <nowiki>?xml version="1.0"?</nowiki>
  <nowiki><</nowiki>?xml version="1.0"?<nowiki>></nowiki>
  <nowiki>!-- initially, the default namespace is "books" --</nowiki>
  <nowiki><</nowiki>!-- initially, the default namespace is "books" --<nowiki>></nowiki>
  <nowiki>book xmlns=<nowiki>’</nowiki>urn:loc.gov:books<nowiki>’</nowiki> xmlns:isbn=<nowiki>’</nowiki>urn:ISBN:0-395-36341-6<nowiki>’></nowiki>
  <nowiki><</nowiki>book xmlns=<nowiki>’</nowiki>urn:loc.gov:books<nowiki>’</nowiki> xmlns:isbn=<nowiki>’</nowiki>urn:ISBN:0-395-36341-6<nowiki>’></nowiki>
  <nowiki>title</nowiki>Cheaper by the Dozen<nowiki>/title</nowiki>
  <nowiki><</nowiki>title<nowiki>></nowiki>Cheaper by the Dozen<nowiki><</nowiki>/title<nowiki>></nowiki>
  <nowiki>isbn:number</nowiki>1568491379<nowiki>/isbn:number</nowiki>
  <nowiki><</nowiki>isbn:number<nowiki>></nowiki>1568491379<nowiki><</nowiki>/isbn:number<nowiki>></nowiki>
  <nowiki>notes</nowiki>
  <nowiki><</nowiki>notes<nowiki>></nowiki>
  <nowiki>!-- make HTML the default namespace for some commentary --</nowiki>
  <nowiki><</nowiki>!-- make HTML the default namespace for some commentary --<nowiki>></nowiki>
  <nowiki>p xmlns=<nowiki>’</nowiki>http://www.w3.org/1999/xhtml<nowiki>’></nowiki> This is a <nowiki>i</nowiki>funny<nowiki>/i</nowiki> book!
  <nowiki><</nowiki>p xmlns=<nowiki>’</nowiki>http://www.w3.org/1999/xhtml<nowiki>’></nowiki> This is a <nowiki><</nowiki>i<nowiki>></nowiki>funny<nowiki><</nowiki>/i<nowiki>></nowiki> book!
     <nowiki>/p</nowiki>
     <nowiki><</nowiki>/p<nowiki>></nowiki>
   <nowiki>/notes</nowiki>
   <nowiki><</nowiki>/notes<nowiki>></nowiki>
  <nowiki>/book</nowiki>
  <nowiki><</nowiki>/book<nowiki>></nowiki>


; XSLT namespace example
; XSLT namespace example


# XSLT instructions are prefixed with "xsl" here
# XSLT instructions are prefixed with "xsl" here
# Output is namespace-less here ...)
# Output is namespace-less here (see the XSLT module for explanations ...)
 
  <nowiki><</nowiki>?xml version="1.0"?<nowiki>></nowiki>
  <nowiki>?xml version="1.0"?</nowiki>
  <nowiki><</nowiki>xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"<nowiki>></nowiki>
  <nowiki>xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"</nowiki>
  <nowiki><</nowiki>xsl:template match="page"<nowiki>></nowiki>
  <nowiki>xsl:template match="page"</nowiki>
  .....
  .....
  <nowiki>html</nowiki>   
  <nowiki><</nowiki>html<nowiki>></nowiki>   
   <nowiki>head</nowiki> <nowiki>title</nowiki> <nowiki>xsl:value-of select="title"/</nowiki> <nowiki>/title</nowiki><nowiki>/head</nowiki>
   <nowiki><</nowiki>head<nowiki>></nowiki> <nowiki><</nowiki>title<nowiki>></nowiki> <nowiki><</nowiki>xsl:value-of select="title"/<nowiki>></nowiki> <nowiki><</nowiki>/title<nowiki>></nowiki><nowiki><</nowiki>/head<nowiki>></nowiki>
  <nowiki>body bgcolor="<nowiki> </nowiki>ffffff"</nowiki>   
  <nowiki><</nowiki>body bgcolor="<nowiki> </nowiki>ffffff"<nowiki>></nowiki>   
   <nowiki>xsl:apply-templates/</nowiki>   
   <nowiki><</nowiki>xsl:apply-templates/<nowiki>></nowiki>   
     <nowiki>/body</nowiki>
     <nowiki><</nowiki>/body<nowiki>></nowiki>
   <nowiki>/html</nowiki>
   <nowiki><</nowiki>/html<nowiki>></nowiki>
  <nowiki>/xsl:template</nowiki>
  <nowiki><</nowiki>/xsl:template<nowiki>></nowiki>
  <nowiki>xsl:template match="title"</nowiki>
  <nowiki><</nowiki>xsl:template match="title"<nowiki>></nowiki>
   <nowiki>h1 align="center"</nowiki>   
   <nowiki><</nowiki>h1 align="center"<nowiki>></nowiki>   
   <nowiki>xsl:apply-templates/</nowiki>   
   <nowiki><</nowiki>xsl:apply-templates/<nowiki>></nowiki>   
   <nowiki>/h1</nowiki>
   <nowiki><</nowiki>/h1<nowiki>></nowiki>
  <nowiki>/xsl:template</nowiki>
  <nowiki><</nowiki>/xsl:template<nowiki>></nowiki>


  <nowiki>xsl:template match="content"</nowiki>
  <nowiki><</nowiki>xsl:template match="content"<nowiki>></nowiki>
   <nowiki>p align="center"</nowiki>   
   <nowiki><</nowiki>p align="center"<nowiki>></nowiki>   
   <nowiki>xsl:apply-templates/</nowiki>   
   <nowiki><</nowiki>xsl:apply-templates/<nowiki>></nowiki>   
   <nowiki>/p</nowiki>
   <nowiki><</nowiki>/p<nowiki>></nowiki>
  <nowiki>/xsl:template</nowiki>
  <nowiki><</nowiki>/xsl:template<nowiki>></nowiki>


  <nowiki>xsl:template match="comment"</nowiki>
  <nowiki><</nowiki>xsl:template match="comment"<nowiki>></nowiki>
   <nowiki>hr /</nowiki>
   <nowiki><</nowiki>hr /<nowiki>></nowiki>
   <nowiki>xsl:apply-templates/</nowiki>
   <nowiki><</nowiki>xsl:apply-templates/<nowiki>></nowiki>
   <nowiki>/xsl:template</nowiki>
   <nowiki><</nowiki>/xsl:template<nowiki>></nowiki>
   <nowiki>/xsl:stylesheet</nowiki>
   <nowiki><</nowiki>/xsl:stylesheet<nowiki>></nowiki>


=== Validation of composite documents ===
=== Validation of composite documents ===
Line 204: Line 202:
# We declare the namespace in the root element, but also could have done it just for the "el" element...
# We declare the namespace in the root element, but also could have done it just for the "el" element...


  <nowiki>!ELEMENT link_list (el<nowiki>+</nowiki>)</nowiki>
  <nowiki><</nowiki>!ELEMENT link_list (el<nowiki>+</nowiki>)<nowiki>></nowiki>
  <nowiki>!ATTLIST link_list xmlns:xlink CDATA <nowiki> </nowiki>FIXED "http://www.w3.org/1999/xlink"</nowiki>
  <nowiki><</nowiki>!ATTLIST link_list xmlns:xlink CDATA <nowiki> </nowiki>FIXED "http://www.w3.org/1999/xlink"<nowiki>></nowiki>
  <nowiki>!ELEMENT el (<nowiki> </nowiki>PCDATA)</nowiki>
  <nowiki><</nowiki>!ELEMENT el (<nowiki> </nowiki>PCDATA)<nowiki>></nowiki>
  <nowiki>!ATTLIST el description CDATA <nowiki> </nowiki>IMPLIED xlink:href CDATA <nowiki> </nowiki>REQUIRED xlink:type CDATA <nowiki> </nowiki>FIXED "simple"</nowiki>
  <nowiki><</nowiki>!ATTLIST el description CDATA <nowiki> </nowiki>IMPLIED xlink:href CDATA <nowiki> </nowiki>REQUIRED xlink:type CDATA <nowiki> </nowiki>FIXED "simple"<nowiki>></nowiki>


; An XML Example file
; An XML Example file


  <nowiki>?xml version="1.0" encoding="ISO-8859-1" ?</nowiki>
  <nowiki><</nowiki>?xml version="1.0" encoding="ISO-8859-1" ?<nowiki>></nowiki>
  <nowiki>!DOCTYPE link_list SYSTEM "link_list.dtd"</nowiki>
  <nowiki><</nowiki>!DOCTYPE link_list SYSTEM "link_list.dtd"<nowiki>></nowiki>
  <nowiki>?xml-stylesheet href="link_list.css" type="text/css" ?</nowiki>
  <nowiki><</nowiki>?xml-stylesheet href="link_list.css" type="text/css" ?<nowiki>></nowiki>
  <nowiki>link_list xmlns:xlink="http://www.w3.org/1999/xlink"</nowiki>
  <nowiki><</nowiki>link_list xmlns:xlink="http://www.w3.org/1999/xlink"<nowiki>></nowiki>
   <nowiki>el description="verified" xlink:type="simple" xlink:href="http://tecfa.unige.ch/"</nowiki>TECFA<nowiki>/el</nowiki>
   <nowiki><</nowiki>el description="verified" xlink:type="simple" xlink:href="http://tecfa.unige.ch/"<nowiki>></nowiki>TECFA<nowiki><</nowiki>/el<nowiki>></nowiki>
   <nowiki>el xlink:href="http://tecfa.unige.ch/" xlink:type="simple"</nowiki>TECFA<nowiki>/el</nowiki>
   <nowiki><</nowiki>el xlink:href="http://tecfa.unige.ch/" xlink:type="simple"<nowiki>></nowiki>TECFA<nowiki><</nowiki>/el<nowiki>></nowiki>
  <nowiki>/link_list</nowiki>
  <nowiki><</nowiki>/link_list<nowiki>></nowiki>


== Client-side XML languages and namespaces overview ==
== Client-side XML languages and namespaces overview ==
Line 301: Line 299:
Workarounds:
Workarounds:
* Use a browser that is more advanced (e.g. Firefox) or specialized players (e.g. Realplayer for SMIL)
* Use a browser that is more advanced (e.g. Firefox) or specialized players (e.g. Realplayer for SMIL)
* Install plugins for your web browser and use a more indirect method to assemble contents, i.e. <nowiki>object</nowiki> or <nowiki>iframe</nowiki> This will work with most browsers if you have the right plugin (SVG in this case) installed.
* Install plugins for your web browser and use a more indirect method to assemble contents, i.e. <nowiki><</nowiki>object<nowiki>></nowiki> or <nowiki><</nowiki>iframe<nowiki>></nowiki> This will work with most browsers if you have the right plugin (SVG in this case) installed.
  <nowiki>iframe src="hello-svg.svg" height="300" width="80%" frameborder="0"</nowiki>
  <nowiki><</nowiki>iframe src="hello-svg.svg" height="300" width="80%" frameborder="0"<nowiki>></nowiki>
... Sorry you need an SVG plugin ...
... Sorry you need an SVG plugin ...
  <nowiki>/iframe</nowiki>
  <nowiki><</nowiki>/iframe<nowiki>></nowiki>


=== XHTML + other vocabularies ===
=== XHTML + other vocabularies ===
Line 319: Line 317:
; Example - Story grammar that implements XLink for an A element
; Example - Story grammar that implements XLink for an A element


  <nowiki>?xml version="1.0" encoding="ISO-8859-1" ?</nowiki>
  <nowiki><</nowiki>?xml version="1.0" encoding="ISO-8859-1" ?<nowiki>></nowiki>
  <nowiki>STORY xmlns:xlink="http://www.w3.org/1999/xlink"</nowiki>
  <nowiki><</nowiki>STORY xmlns:xlink="http://www.w3.org/1999/xlink"<nowiki>></nowiki>
  <nowiki>Title</nowiki>The Webmaster<nowiki>/Title</nowiki>
  <nowiki><</nowiki>Title<nowiki>></nowiki>The Webmaster<nowiki><</nowiki>/Title<nowiki>></nowiki>
  .....
  .....
   <nowiki>INFOS</nowiki>   
   <nowiki><</nowiki>INFOS<nowiki>></nowiki>   
     <nowiki>Date</nowiki>30 octobre 2003<nowiki>/Date</nowiki>
     <nowiki><</nowiki>Date<nowiki>></nowiki>30 octobre 2003<nowiki><</nowiki>/Date<nowiki>></nowiki>
     <nowiki>Author</nowiki>DKS -<nowiki>/Author</nowiki>
     <nowiki><</nowiki>Author<nowiki>></nowiki>DKS -<nowiki><</nowiki>/Author<nowiki>></nowiki>
     <nowiki>A xlink:href=http://jigsaw.w3.org/css-validator/check/referer xlink:type="simple"</nowiki>CSS Validator<nowiki>/A</nowiki>
     <nowiki><</nowiki>A xlink:href=http://jigsaw.w3.org/css-validator/check/referer xlink:type="simple"<nowiki>></nowiki>CSS Validator<nowiki><</nowiki>/A<nowiki>></nowiki>
   <nowiki>/INFOS</nowiki>
   <nowiki><</nowiki>/INFOS<nowiki>></nowiki>
  <nowiki>/STORY</nowiki>
  <nowiki><</nowiki>/STORY<nowiki>></nowiki>


;SVG example
;SVG example
Line 334: Line 332:
XHTML with embedded SVG tags (tested with Firefox)
XHTML with embedded SVG tags (tested with Firefox)


  <nowiki>?xml version="1.0" encoding="ISO-8859-1" ?</nowiki>
  <nowiki><</nowiki>?xml version="1.0" encoding="ISO-8859-1" ?<nowiki>></nowiki>
  <nowiki>!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" </nowiki>
  <nowiki><</nowiki>!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" <nowiki>></nowiki>
  <nowiki>html xmlns="http://www.w3.org/1999/xhtml"
  <nowiki><</nowiki>html xmlns="http://www.w3.org/1999/xhtml"
     xmlns:svg="http://www.w3.org/2000/svg"</nowiki>
     xmlns:svg="http://www.w3.org/2000/svg"<nowiki>></nowiki>
  <nowiki>head</nowiki>   
  <nowiki><</nowiki>head<nowiki>></nowiki>   
  <nowiki>title</nowiki>SVG within XHTML Demo<nowiki>/title</nowiki>   
  <nowiki><</nowiki>title<nowiki>></nowiki>SVG within XHTML Demo<nowiki><</nowiki>/title<nowiki>></nowiki>   
  <nowiki>/head</nowiki>
  <nowiki><</nowiki>/head<nowiki>></nowiki>
  <nowiki>body</nowiki>
  <nowiki><</nowiki>body<nowiki>></nowiki>
  .... The SVG part starts below <nowiki>hr /</nowiki>
  .... The SVG part starts below <nowiki><</nowiki>hr /<nowiki>></nowiki>
  <nowiki>svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="400" height="300"</nowiki>
  <nowiki><</nowiki>svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="400" height="300"<nowiki>></nowiki>
  <nowiki>!-- a small rectangle --</nowiki>
  <nowiki><</nowiki>!-- a small rectangle --<nowiki>></nowiki>
   <nowiki>rect x="50" y="50" rx="5" ry="5" width="300" height="100" style="fill:<nowiki> </nowiki>CCCCFF;stroke:#000099"/</nowiki>
   <nowiki><</nowiki>rect x="50" y="50" rx="5" ry="5" width="300" height="100" style="fill:<nowiki> </nowiki>CCCCFF;stroke:#000099"/<nowiki>></nowiki>
  <nowiki>!-- a text in the same place --</nowiki>
  <nowiki><</nowiki>!-- a text in the same place --<nowiki>></nowiki>
   <nowiki>text x="55" y="90" style="stroke:#000099;fill:#000099;font-size:24;"</nowiki>HELLO dear reader <nowiki>/text</nowiki>
   <nowiki><</nowiki>text x="55" y="90" style="stroke:#000099;fill:#000099;font-size:24;"<nowiki>></nowiki>HELLO dear reader <nowiki><</nowiki>/text<nowiki>></nowiki>
   <nowiki>/svg</nowiki>
   <nowiki><</nowiki>/svg<nowiki>></nowiki>
  <nowiki>hr /</nowiki>
  <nowiki><</nowiki>hr /<nowiki>></nowiki>
  The SVG part ended above
  The SVG part ended above
  <nowiki>/body</nowiki>
  <nowiki><</nowiki>/body<nowiki>></nowiki>
  <nowiki>/html</nowiki>
  <nowiki><</nowiki>/html<nowiki>></nowiki>


=== Combining XHTML with your XML ===
=== Combining XHTML with your XML ===
Line 373: Line 371:


XHTML with some cooking XML included (file cooking.xhtml):
XHTML with some cooking XML included (file cooking.xhtml):
  <nowiki>?xml version="1.0" encoding="iso-8859-1"?</nowiki>
  <nowiki><</nowiki>?xml version="1.0" encoding="iso-8859-1"?<nowiki>></nowiki>
  <nowiki>?xml-stylesheet type="text/css" href="cooking.css"?</nowiki>
  <nowiki><</nowiki>?xml-stylesheet type="text/css" href="cooking.css"?<nowiki>></nowiki>
  <nowiki>html xmlns="http://www.w3.org/1999/xhtml" xmlns:re="http://tecfa.unige.ch/ns/cooking"</nowiki>
  <nowiki><</nowiki>html xmlns="http://www.w3.org/1999/xhtml" xmlns:re="http://tecfa.unige.ch/ns/cooking"<nowiki>></nowiki>
  <nowiki>head</nowiki>
  <nowiki><</nowiki>head<nowiki>></nowiki>
  <nowiki>title</nowiki>A Compound XHTML-XML Document<nowiki>/title</nowiki>
  <nowiki><</nowiki>title<nowiki>></nowiki>A Compound XHTML-XML Document<nowiki><</nowiki>/title<nowiki>></nowiki>
  <nowiki>!-- this should work with IE but doesn<nowiki>’</nowiki>t, maybe IE6 ? --</nowiki>
  <nowiki><</nowiki>!-- this should work with IE but doesn<nowiki>’</nowiki>t, maybe IE6 ? --<nowiki>></nowiki>
  <nowiki>link href="cooking.css" type="text/css" rel="stylesheet"/</nowiki>
  <nowiki><</nowiki>link href="cooking.css" type="text/css" rel="stylesheet"/<nowiki>></nowiki>
  <nowiki>/head</nowiki>
  <nowiki><</nowiki>/head<nowiki>></nowiki>
  <nowiki>body</nowiki>
  <nowiki><</nowiki>body<nowiki>></nowiki>
  <nowiki>h1</nowiki>A Compound XHTML-XML Document<nowiki>/h1</nowiki>
  <nowiki><</nowiki>h1<nowiki>></nowiki>A Compound XHTML-XML Document<nowiki><</nowiki>/h1<nowiki>></nowiki>
  <nowiki>p</nowiki>Contains a cooking recipee written down by R. D. as an exercise.
  <nowiki><</nowiki>p<nowiki>></nowiki>Contains a cooking recipee written down by R. D. as an exercise.
   Everything below the line is XML styled with a CSS stylesheet<nowiki>/ p</nowiki>
   Everything below the line is XML styled with a CSS stylesheet<nowiki><</nowiki>/ p<nowiki>></nowiki>
  <nowiki>hr/</nowiki>
  <nowiki><</nowiki>hr/<nowiki>></nowiki>
  <nowiki>re:recipe</nowiki>
  <nowiki><</nowiki>re:recipe<nowiki>></nowiki>
  <nowiki>re:recipe_head</nowiki>
  <nowiki><</nowiki>re:recipe_head<nowiki>></nowiki>
   <nowiki>re:recipe_name</nowiki>Cold Salmon in Creamy Spiced Sauce<nowiki>/re:recipe_name</nowiki>
   <nowiki><</nowiki>re:recipe_name<nowiki>></nowiki>Cold Salmon in Creamy Spiced Sauce<nowiki><</nowiki>/re:recipe_name<nowiki>></nowiki>
   <nowiki>re:recipe_author</nowiki>H.W.<nowiki>/re:recipe_author</nowiki>
   <nowiki><</nowiki>re:recipe_author<nowiki>></nowiki>H.W.<nowiki><</nowiki>/re:recipe_author<nowiki>></nowiki>
   <nowiki>re:meal_type</nowiki>Fish and Shellfish<nowiki>/re:meal_type</nowiki>
   <nowiki><</nowiki>re:meal_type<nowiki>></nowiki>Fish and Shellfish<nowiki><</nowiki>/re:meal_type<nowiki>></nowiki>
  <nowiki>/re:recipe_head</nowiki>
  <nowiki><</nowiki>/re:recipe_head<nowiki>></nowiki>
  .....
  .....
  <nowiki>/re:recipe</nowiki>
  <nowiki><</nowiki>/re:recipe<nowiki>></nowiki>
  <nowiki>/body</nowiki>
  <nowiki><</nowiki>/body<nowiki>></nowiki>
  <nowiki>/html</nowiki>
  <nowiki><</nowiki>/html<nowiki>></nowiki>


=== Combining your XML with XHTML ===
=== Combining your XML with XHTML ===
Line 404: Line 402:


;XML plus XHTML (file xml_plus_xhtml.xml)
;XML plus XHTML (file xml_plus_xhtml.xml)
  <nowiki>?xml version="1.0" ?</nowiki>
  <nowiki><</nowiki>?xml version="1.0" ?<nowiki>></nowiki>
  <nowiki>?xml-stylesheet href="xml_plus_xhtml.css" type="text/css"?</nowiki>
  <nowiki><</nowiki>?xml-stylesheet href="xml_plus_xhtml.css" type="text/css"?<nowiki>></nowiki>
  <nowiki>page xmlns:html="http://www.w3.org/1999/xhtml" updated="jan 2007"</nowiki>
  <nowiki><</nowiki>page xmlns:html="http://www.w3.org/1999/xhtml" updated="jan 2007"<nowiki>></nowiki>
  <nowiki>title</nowiki>Hello friend<nowiki>/title</nowiki>
  <nowiki><</nowiki>title<nowiki>></nowiki>Hello friend<nowiki><</nowiki>/title<nowiki>></nowiki>
  <nowiki>list</nowiki>
  <nowiki><</nowiki>list<nowiki>></nowiki>
   <nowiki>!-- we use an HTML tag below to include a picture --</nowiki>
   <nowiki><</nowiki>!-- we use an HTML tag below to include a picture --<nowiki>></nowiki>
   <nowiki>html:img src="photo.jpg"/</nowiki>
   <nowiki><</nowiki>html:img src="photo.jpg"/<nowiki>></nowiki>
   <nowiki>item price="10"</nowiki> White plate  <nowiki>/item</nowiki>
   <nowiki><</nowiki>item price="10"<nowiki>></nowiki> White plate  <nowiki><</nowiki>/item<nowiki>></nowiki>
   <nowiki>item price="20"</nowiki> Gold plate  <nowiki>/item</nowiki>
   <nowiki><</nowiki>item price="20"<nowiki>></nowiki> Gold plate  <nowiki><</nowiki>/item<nowiki>></nowiki>
   <nowiki>item price="15"</nowiki> Silver plate  <nowiki>/item</nowiki>
   <nowiki><</nowiki>item price="15"<nowiki>></nowiki> Silver plate  <nowiki><</nowiki>/item<nowiki>></nowiki>
  <nowiki>/list</nowiki>
  <nowiki><</nowiki>/list<nowiki>></nowiki>
  <nowiki>comment</nowiki> Written by <nowiki>html:a href="...."</nowiki>DKS/Tecfa<nowiki>/html:a</nowiki> , feb 2007 <nowiki>/comment</nowiki>
  <nowiki><</nowiki>comment<nowiki>></nowiki> Written by <nowiki><</nowiki>html:a href="...."<nowiki>></nowiki>DKS/Tecfa<nowiki><</nowiki>/html:a<nowiki>></nowiki> , feb 2007 <nowiki><</nowiki>/comment<nowiki>></nowiki>
  <nowiki>/page</nowiki>
  <nowiki><</nowiki>/page<nowiki>></nowiki>


[[Category: Technologies]]
[[Category: Technologies]]
[[Category: XML]]
[[Category: XML]]
[[Category: Tutorials]]
[[Category: Tutorials]]

Revision as of 10:36, 4 May 2007

This is a draft for a tutorial article, but it needs to be fixed in several places.

Note on its production: It's a Framemaker to Word to Mediawiki translation from parts of teaching slides - 21:50, 20 March 2007 (MET)

Definition

XML Namespaces allow to fully/uniquely qualify XML element and attribute names to prevent from confusing two elements that have the same name but mean different things.

Purpose and history of XML namespace

Various XML vocabularies (languages/applications) can be mixed. But then we can run into naming conflicts, i.e. different vocabularies (DTDs) can use the same names for elements !

Since DTDs are not mandatory for most W3C languages, the W3C requires namespace definitions within root elements, e.g. XHTML must have one:

<html xmlns="http://www.w3.org/1999/xhtml">

Namespaces are a W3C standard since Jan 1999. That means that namespaces have been introduced after XML has been invented.

Standards involved

The Namespace standard:

Namespaces are identified with URIs

A URI is either a URL or a URN

Compound W3C documents

See also: The standards overview article. Most XML-based standards in education (e.g. IMS Content Packaging make heavy use of namespaces ! In addition most IMS and SCORM standards use XML Schema which are associated to XML contents via namespace declarations.

Namespaces

An example demonstrating the need for namespaces

Here is an XML fragment using title within a bibliography element:

<book>
 <title>A true story</title>
 <description>A real cool publication</description>
</book>

A 2nd XML fragment uses title in a employees record:

<record>
 <name>Miller</name>  <title>Dr. </title>
 <publications> ...  </publications>
</record>

If these two XML fragments were added together, there would be a name conflict because both contain a <title> element with different meaning. But we can solve this like this:

<employees:record>
<employees:name>Miller</employees:name>
<employees:title>Dr.  </employees:title>
<employees:publications>
 <biblio:book>
  <biblio:title>A true story</biblio:title>
  <biblio:description>A real cool publication</biblio:description>
 </biblio:book>
</employees:publications>
</employees:record>

Declaring namespaces

Formally speaking, an XML namespace is simply a collection of names (elements and attributes) of a markup vocabulary that can be uniquely identified

Procedure
  1. Create or identify a namespace identifier you wish to use: An XML namespace is identified by a unique URI reference, usually a URL.
  2. The URL need not point to anything on the Internet. It is just used as a unique string, i.e. a name
  3. Therefore if some namespace URLs below are broken there is absolutly nothing to worry about. However, most namespace identifiers actually point to a real web page that either provides an explanation or at least informations about the organization. This means that if you plan to create your own namespaces, you should use the name of any webpage over which you have control.
  4. Make a namespace declaration within the element that belongs to this namespace, i.e. map a prefix of your choice to a unique URI.

There are two major variants to declare namespaces

(1) declaring a namespace that will require insertion of prefixes

<prefix:element xmlns:prefix="URI">

e.g.

<html:html xmlns:html=’http://www.w3.org/1999/xhtml’>  

(2) declaring a default namespace (element + children belong to this namespace by default)

<element xmlns="URI">

e.g.

<html xmlns=’http://www.w3.org/1999/xhtml’>  .....

Scoping

"Scoping" means "where does a declaration apply ?

The scope of an XML namespace declaration extends from the beginning of the start-tag in which it appears to the end of the corresponding end-tag, including all children. Most frequently, namespaces are declared in the document root.

XHMTL fragment example
<?xml version="1.0"?>
<html:html xmlns:html=’http://www.w3.org/1999/xhtml’>
 <html:head><html:title>Frobnostication</html:title></html:head>
 <html:body><html:p>Moved to
  <html:a href=’http://frob.example.com’>here.</html:a></html:p>
 </html:body>
</html:html>
Example with 2 namespaces (both use URNs instead of URLs)
<?xml version="1.0"?>
<!-- both namespace prefixes are available throughout -->
<bk:book xmlns:bk=’urn:loc.gov:books’ xmlns:isbn=’urn:ISBN:0-395-36341-6’>
  <bk:title>Cheaper by the Dozen</bk:title>
  <isbn:number>1568491379</isbn:number>
</bk:book>

Default namespaces

If most elements in an XML document belonged to the same namespace, it would be ugly to prefix each element name. Instead define a default namespace that applies to all non-prefixed elements and attributes.

<element xmlns="URI"> ....

The default namespace applies to the element on which it was defined and all descendants of that element. But if a descendants has another default namespace defined on it, this new namespace definition overrides the previous one and becomes the default for that element and its descendants. Note that namespaces does not apply to attribute names ! This is a likely source of trouble for XSLT ...

XHTML default namespace example
<?xml version="1.0"?>
<!-- elements are in the HTML namespace by default -->
 <html xmlns=’http://www.w3.org/1999/xhtml’>
 <head><title>Frobnostication</title></head>
 <body><p>Moved to
 <a href=’http://frob.example.com’>here</a>.</p></body>
</html>

Default namespace for a book vocabulary plus a namespace for ISBN example:

<?xml version="1.0"?>
<!-- unprefixed element types are from "books" -->
  <book  xmlns=’urn:loc.gov:books’
xmlns:isbn=’urn:ISBN:0-395-36341-6’>
    <title>Cheaper by the Dozen</title>
    <isbn:number>1568491379</isbn:number>
  </book>
A larger example of namespace scoping

It includes some XHTML whose namespace is declared within the "p" element.

<?xml version="1.0"?>
<!-- initially, the default namespace is "books" -->
<book xmlns=’urn:loc.gov:books’ xmlns:isbn=’urn:ISBN:0-395-36341-6’>
<title>Cheaper by the Dozen</title>
<isbn:number>1568491379</isbn:number>
<notes>
<!-- make HTML the default namespace for some commentary -->
<p xmlns=’http://www.w3.org/1999/xhtml’> This is a <i>funny</i> book!
   </p>
 </notes>
</book>
XSLT namespace example
  1. XSLT instructions are prefixed with "xsl" here
  2. Output is namespace-less here (see the XSLT module for explanations ...)
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="page">
.....
<html>   
 <head> <title> <xsl:value-of select="title"/> </title></head>
<body bgcolor=" ffffff">   
  <xsl:apply-templates/>  
    </body>
  </html>
</xsl:template>
<xsl:template match="title">
 <h1 align="center">  
 <xsl:apply-templates/>  
 </h1>
</xsl:template>
<xsl:template match="content">
 <p align="center">  
 <xsl:apply-templates/>  
 </p>
</xsl:template>
<xsl:template match="comment">
 <hr />
 <xsl:apply-templates/>
 </xsl:template>
 </xsl:stylesheet>

Validation of composite documents

  1. Validating documents that contain different namespaces are called composite or compound documents and validation is not always easy since combined DTDs may not exist
  2. A validation standard for composite W3C vocabularies is currently under preparation ...
  3. Usually, combined documents are produced by server-side programs for delivery and they are just well-formed (not attached to DTDs or other schemas)
  4. You may write DTDs that validate compound documents.
A DTD rule with an extra namespace example
  1. In this example, the "a" element and the "description" attribute belongs to the default namespace, but the href and type attributes belong to the xlink namespace
  2. We declare the namespace in the root element, but also could have done it just for the "el" element...
<!ELEMENT link_list (el+)>
<!ATTLIST link_list xmlns:xlink CDATA  FIXED "http://www.w3.org/1999/xlink">
<!ELEMENT el ( PCDATA)>
<!ATTLIST el description CDATA  IMPLIED xlink:href CDATA  REQUIRED xlink:type CDATA  FIXED "simple">
An XML Example file
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE link_list SYSTEM "link_list.dtd">
<?xml-stylesheet href="link_list.css" type="text/css" ?>
<link_list xmlns:xlink="http://www.w3.org/1999/xlink">
 <el description="verified" xlink:type="simple" xlink:href="http://tecfa.unige.ch/">TECFA</el>
 <el xlink:href="http://tecfa.unige.ch/" xlink:type="simple">TECFA</el>
</link_list>

Client-side XML languages and namespaces overview

The W3C defines several Internet languages and for which namespace declarations are mandatory (even if you use them "standalone", i.e. not in composite documents.

Some W3C languages
Name Namespace URL Explanation
XSLT http://www.w3.org/1999/XSL/Transform XSL - Transformations
XSL-FO http://www.w3.org/1999/XSL/Format XSL - Formatting
XML Schema http://www.w3.org/2001/XMLSchema  
SVG http://www.w3.org/2000/svg Scalable Vector Graphics
RDF http://www.w3.org/1999/02/22-rdf-syntax-ns Resource Description Format
SMIL 2.0 http://www.w3.org/2001/SMIL20/ Synchronized Multimedia Integration Lang.
VoiceXML 2.0 http://www.w3.org/2001/vxml Synthesized voice
XLink xmlns:xlink="http://www.w3.org/1999/xlink" XML Linking Language
XForms http://www.w3.org/2002/xforms Next generation of HTML forms.
XHTML http://www.w3.org/1999/xhtml  
MathML http://www.w3.org/1998/Math/MathML Mathematical markup language


Some of these languages (e.g. SMIL, MathML, SVG, X3D) need special clients for display. Others are transducers or helpers, i.e. are used to define, style, transform and process XML contents

RDF namespace

RDF-based languages are very prolific and add to the complexity of semantic web initiatives

See 100 most common RDF namespaces

Compound documents

Combining various content delivery formats is the future (also on cell phones). For example, XHTML-formatted content can be augmented by SVG objects or mathematical formula.

Examples of possible Compound Document profiles
  • XHTML + SVG + MathML
  • XHTML + SMIL
  • XHTML + XForms
  • XHTML + VoiceML

The W3C is working on a generic Compound Document by Reference Framework (CDRF) that defines a language-independent processing model for combining arbitrary document formats ...

Web browser caveats
  • IE doesn't support natively most of these languages, Firefox supports more but not all.

Workarounds:

  • Use a browser that is more advanced (e.g. Firefox) or specialized players (e.g. Realplayer for SMIL)
  • Install plugins for your web browser and use a more indirect method to assemble contents, i.e. <object> or <iframe> This will work with most browsers if you have the right plugin (SVG in this case) installed.
<iframe src="hello-svg.svg" height="300" width="80%" frameborder="0">

... Sorry you need an SVG plugin ...

</iframe>

XHTML + other vocabularies

XHTML / MathML example

See the MathML article. It contains some mixed XHTML/MathML code.

Xlink example

XLink was supposed to replace all linking constructs in the W3C languages. It is adopted by SVG, X3D, but not by XHTML 2 which introduces its own linking constructs. XML-capable browsers are also supposed to implement this (Firefox, but not IE)

Example - Story grammar that implements XLink for an A element
<?xml version="1.0" encoding="ISO-8859-1" ?>
<STORY xmlns:xlink="http://www.w3.org/1999/xlink">
<Title>The Webmaster</Title>
.....
  <INFOS>   
   <Date>30 octobre 2003</Date>
   <Author>DKS -</Author>
   <A xlink:href=http://jigsaw.w3.org/css-validator/check/referer xlink:type="simple">CSS Validator</A>
 </INFOS>
</STORY>
SVG example

XHTML with embedded SVG tags (tested with Firefox)

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:svg="http://www.w3.org/2000/svg">
<head>  
<title>SVG within XHTML Demo</title>  
</head>
<body>
.... The SVG part starts below <hr />
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="400" height="300">
<!-- a small rectangle -->
  <rect x="50" y="50" rx="5" ry="5" width="300" height="100" style="fill: CCCCFF;stroke:#000099"/>
<!-- a text in the same place -->
  <text x="55" y="90" style="stroke:#000099;fill:#000099;font-size:24;">HELLO dear reader </text>
 </svg>
<hr />
The SVG part ended above
</body>
</html>

Combining XHTML with your XML

It sometimes can be practical to combine your own XML contents with XHTML, because HTML offers easy-to-use functionality like image inclusions or links. Note however that in practice this feature is not often used since:

  • XSLT stylesheets allow transformations of XML data into XHTML (i.e. the stylesheet will contain the extra HTML and the result displayed usually will be all HTML and easier to manage with CSS)
  • the way browsers implement this is not really stable
XHTML with some XML included and CSS example
  1. Tested with Firefox 2.x (works) and IE 7 (CSS doesn’t work, I don’t know why ...)

Procedure:

  1. Define a namespace for the included XML content
  2. Attach a CSS stylesheet with an XML processor instruction
  3. Your file must be understood as "XML", e.g. call it something.xml or something.xhtml but not something.html !


XHTML with some cooking XML included (file cooking.xhtml):

<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/css" href="cooking.css"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:re="http://tecfa.unige.ch/ns/cooking">
<head>
<title>A Compound XHTML-XML Document</title>
<!-- this should work with IE but doesn’t, maybe IE6 ? -->
<link href="cooking.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<h1>A Compound XHTML-XML Document</h1>
<p>Contains a cooking recipee written down by R. D. as an exercise.
 Everything below the line is XML styled with a CSS stylesheet</ p>
<hr/>
<re:recipe>
<re:recipe_head>
 <re:recipe_name>Cold Salmon in Creamy Spiced Sauce</re:recipe_name>
 <re:recipe_author>H.W.</re:recipe_author>
 <re:meal_type>Fish and Shellfish</re:meal_type>
</re:recipe_head>
.....
</re:recipe>
</body>
</html>

Combining your XML with XHTML

Works with Firefox 1x/2x and IE7 (probably also with IE6).

  • An HTML namespace is declared in the root element and we use it twice (for the img and a tags).
  • Btw this is a trick to get around non-implementation of the xlink standard in IE.
XML plus XHTML (file xml_plus_xhtml.xml)
<?xml version="1.0" ?>
<?xml-stylesheet href="xml_plus_xhtml.css" type="text/css"?>
<page xmlns:html="http://www.w3.org/1999/xhtml" updated="jan 2007">
<title>Hello friend</title>
<list>
  <!-- we use an HTML tag below to include a picture -->
  <html:img src="photo.jpg"/>
  <item price="10"> White plate  </item>
  <item price="20"> Gold plate  </item>
  <item price="15"> Silver plate  </item>
</list>
<comment> Written by <html:a href="....">DKS/Tecfa</html:a> , feb 2007 </comment>
</page>