XML namespace: Difference between revisions

The educational technology and digital learning wiki
Jump to navigation Jump to search
Line 358: Line 358:
|}<br clear="all">
|}<br clear="all">


# Some of these languages (e.g. SMIL, MathML, SVG, X3D) need special clients for display
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
# Some of these languages are transducers or helpers, i.e. are used to define, style, transform and process XML contents


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


'''Compound documents'''
* XHTML <nowiki>+</nowiki> SVG <nowiki>+</nowiki> MathML
* XHTML <nowiki>+</nowiki> SMIL
* XHTML <nowiki>+</nowiki> XForms
* XHTML <nowiki>+</nowiki> 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 ...


# Combining various content delivery formats is the future (also on cell phones !)
;Web browser caveats:
# For example, XHTML-formatted content can be augmented by SVG objects or mathematical formula.
# Examples of possible Compound Document profiles:
# XHTML <nowiki>+</nowiki> SVG <nowiki>+</nowiki> MathML
# XHTML <nowiki>+</nowiki> SMIL
# XHTML <nowiki>+</nowiki> XForms
# XHTML <nowiki>+</nowiki> 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<nowiki>’</nowiki>t support natively most of these languages, Firefox supports more but not all.
* IE doesn't support natively most of these languages, Firefox supports more but not all.
# Workaround:
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><</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.
* 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><</nowiki>iframe src="hello-svg.svg" height="300" width="80%" frameborder="0"<nowiki>></nowiki>
<nowiki><</nowiki>iframe src="hello-svg.svg" height="300" width="80%" frameborder="0"<nowiki>></nowiki>


Line 391: Line 385:
<nowiki><</nowiki>/iframe<nowiki>></nowiki>
<nowiki><</nowiki>/iframe<nowiki>></nowiki>


----
#######
----
'''MathML example'''
----
####### '''XHMTL <nowiki>+</nowiki> MathML (file xhtml_mathml.xml)'''
----
# XHTML is declared in the root since the document is XHTML
# MathML is declared when we use it (could have been declared in the root too)
<nowiki><</nowiki>?xml version="1.0" encoding="iso-8859-1"?<nowiki>></nowiki>
<nowiki><</nowiki>'''xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml"<nowiki>></nowiki>'''
<nowiki><</nowiki>xhtml:body<nowiki>></nowiki>
<nowiki><</nowiki>xhtml:h1<nowiki>></nowiki>A Compound Document<nowiki><</nowiki>/xhtml:h1<nowiki>></nowiki>
<nowiki><</nowiki>xhtml:p<nowiki>></nowiki>A simple formula using MathML in XHTML.<nowiki><</nowiki>/xhtml:p<nowiki>></nowiki>
<nowiki><</nowiki>'''mathml:math xmlns:mathml="http://www.w3.org/1998/Math/MathML"<nowiki>></nowiki>'''
<nowiki><</nowiki>mathml:mrow<nowiki>></nowiki>
<nowiki><</nowiki>mathml:msqrt<nowiki>></nowiki>
<nowiki><</nowiki>mathml:mn<nowiki>></nowiki>49<nowiki><</nowiki>/mathml:mn<nowiki>></nowiki>
<nowiki><</nowiki>/mathml:msqrt<nowiki>></nowiki>
<nowiki><</nowiki>mathml:mo<nowiki>></nowiki>=<nowiki><</nowiki>/mathml:mo<nowiki>></nowiki>
<nowiki><</nowiki>mathml:mn<nowiki>></nowiki>7<nowiki><</nowiki>/mathml:mn<nowiki>></nowiki>
<nowiki><</nowiki>/mathml:mrow<nowiki>></nowiki>
<nowiki><</nowiki>/mathml:math<nowiki>></nowiki>
<nowiki><</nowiki>/xhtml:body<nowiki>></nowiki>
<nowiki><</nowiki>/xhtml:html<nowiki>></nowiki>
# Warning: Does not work with IE 6/7, but there are workarounds:
# use the W3C MathML stylesheet that also will install an appropriate IE plugin (see ex.  <nowiki>[</nowiki>-18<nowiki>]</nowiki>)
# use e.g. Firefox or Amaya
# or install a MathML plugin for IE and include MathML contents with an iframe or similar construct
----
####### '''XHMTL <nowiki>+</nowiki> MathML (file xhtml_mathml2.xml)'''
----
# In the previous example, one also could have declared xhtml as default namespace and overide the default namespace just for the mathml "section". The result is the same
<nowiki><</nowiki>?xml version="1.0" encoding="iso-8859-1"?<nowiki>></nowiki>
<nowiki><</nowiki>html xmlns="http://www.w3.org/1999/xhtml"<nowiki>></nowiki>
<nowiki><</nowiki>body<nowiki>></nowiki>
<nowiki><</nowiki>h1<nowiki>></nowiki>A Compound Document<nowiki><</nowiki>/h1<nowiki>></nowiki>
<nowiki><</nowiki>p<nowiki>></nowiki>A simple formula using MathML in XHTML.<nowiki><</nowiki>/p<nowiki>></nowiki>
<nowiki><</nowiki>mathml:math xmlns:mathml="http://www.w3.org/1998/Math/MathML"<nowiki>></nowiki>
<nowiki><</nowiki>mathml:mrow<nowiki>></nowiki>
<nowiki><</nowiki>mathml:msqrt<nowiki>></nowiki>
<nowiki><</nowiki>mathml:mn<nowiki>></nowiki>49<nowiki><</nowiki>/mathml:mn<nowiki>></nowiki>
<nowiki><</nowiki>/mathml:msqrt<nowiki>></nowiki>
<nowiki><</nowiki>mathml:mo<nowiki>></nowiki>=<nowiki><</nowiki>/mathml:mo<nowiki>></nowiki>
<nowiki><</nowiki>mathml:mn<nowiki>></nowiki>7<nowiki><</nowiki>/mathml:mn<nowiki>></nowiki>


<nowiki><</nowiki>/mathml:mrow<nowiki>></nowiki>
; XHTML / MathML example


<nowiki><</nowiki>/mathml:math<nowiki>></nowiki>
See the [[MathML]] article. It contains some mixed XHTML/MathML code.


<nowiki><</nowiki>/body<nowiki>></nowiki>


<nowiki><</nowiki>/html<nowiki>></nowiki>
;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)


####### '''XHMTL <nowiki>+</nowiki> MathML <nowiki>+</nowiki> XSLT for IE (file xhtml_mathml_IE.xml)'''
<nowiki><</nowiki>?xml version="1.0"?<nowiki>></nowiki>


'''<nowiki><</nowiki>?xml-stylesheet type="text/xsl" href="pmathml.xsl"?<nowiki>></nowiki>'''
; Example: Story grammar that implements XLink for an A element
 
''' <nowiki><</nowiki>html xmlns="http://www.w3.org/1999/xhtml"'''
 
'''      xmlns:pref="http://www.w3.org/2002/Math/preference"'''
 
'''      pref:renderer="css"<nowiki>></nowiki>'''
 
<nowiki><</nowiki>head<nowiki>></nowiki>  <nowiki><</nowiki>title<nowiki>></nowiki>MATHML with an XSLT trick for IE<nowiki><</nowiki>/title<nowiki>></nowiki> <nowiki><</nowiki>/head<nowiki>></nowiki>
 
<nowiki><</nowiki>body<nowiki>><</nowiki>h1<nowiki>></nowiki>MATHML with an XSLT trick for IE<nowiki><</nowiki>/h1<nowiki>></nowiki>
 
<nowiki><</nowiki>p<nowiki>></nowiki>Below is an equation with a radical:<nowiki><</nowiki>/p<nowiki>></nowiki>
 
<nowiki><</nowiki>math xmlns="http://www.w3.org/1998/Math/MathML"<nowiki>></nowiki>
 
<nowiki><</nowiki>msup<nowiki>></nowiki>
 
<nowiki><</nowiki>msqrt<nowiki>></nowiki>
 
<nowiki><</nowiki>mrow<nowiki>></nowiki>
 
<nowiki><</nowiki>mi<nowiki>></nowiki>a<nowiki><</nowiki>/mi<nowiki>></nowiki>
 
<nowiki><</nowiki>mo<nowiki>>+<</nowiki>/mo<nowiki>></nowiki>
 
<nowiki><</nowiki>mi<nowiki>></nowiki>b<nowiki><</nowiki>/mi<nowiki>></nowiki>
 
<nowiki><</nowiki>/mrow<nowiki>></nowiki>
 
<nowiki><</nowiki>/msqrt<nowiki>></nowiki>
 
<nowiki><</nowiki>mn<nowiki>></nowiki>27<nowiki><</nowiki>/mn<nowiki>></nowiki>
 
<nowiki><</nowiki>/msup<nowiki>></nowiki>
 
<nowiki><</nowiki>/math<nowiki>></nowiki>
 
<nowiki><</nowiki>/body<nowiki>><</nowiki>/html<nowiki>></nowiki>
 
----
 
# To make this work, you need to copy the mathml.xsl, pmathml.xsl, ctop.xsl and pmathmlcss.xsl files in the same directory
 
<font color="#0000FF"><u>'''----'''</u></font>
 
# <font color="#0000FF"><u>'''http://www.w3.org/Math/XSL/</u> </font>''''''(consult for further details)'''
 
----
 
#######
 
----
 
'''Xlink example'''
 
----
 
# XLink was supposed to replace all linking constructs in the W3C languages
# 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)
 
----
 
####### '''Story grammar that implements XLink for an A element'''


<nowiki><</nowiki>?xml version="1.0" encoding="ISO-8859-1" ?<nowiki>></nowiki>
<nowiki><</nowiki>?xml version="1.0" encoding="ISO-8859-1" ?<nowiki>></nowiki>
Line 574: Line 419:
<nowiki><</nowiki>/STORY<nowiki>></nowiki>
<nowiki><</nowiki>/STORY<nowiki>></nowiki>


----
;SVG example
 
#######
 
----
 
'''SVG example'''


----
XHTML with embedded SVG tags (tested with Firefox)


####### '''XHTML with embedded SVG tags (file xhtml_svg.xhtml)'''
----
# Tested with Firefox
<nowiki><</nowiki>?xml version="1.0" encoding="ISO-8859-1" ?<nowiki>></nowiki>
<nowiki><</nowiki>?xml version="1.0" encoding="ISO-8859-1" ?<nowiki>></nowiki>


Line 631: Line 465:
----
----


####### '''SVG with a plugin (file html_svg.html)'''


----
=== Combining your XML with XHTML ===


# Works with IE or Firefox (and a SVG plugin installed)
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.
<nowiki><</nowiki>!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"<nowiki>></nowiki>
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


<nowiki><</nowiki>html<nowiki>></nowiki>


<nowiki><</nowiki>head<nowiki>></nowiki> <nowiki><</nowiki>title<nowiki>></nowiki>HELLO SVG with an iframe<nowiki><</nowiki>/title<nowiki>></nowiki> <nowiki><</nowiki>/head<nowiki>></nowiki>
;XHTML with some XML included and CSS example


<nowiki><</nowiki>body<nowiki>></nowiki>
# Tested with Firefox 2.x (works) and IE 7 (CSS doesn<nowiki></nowiki>t work, I don<nowiki></nowiki>t know why ...)
 
<nowiki><</nowiki>h1<nowiki>></nowiki>HELLO SVG with an iframe<nowiki><</nowiki>/h1<nowiki>></nowiki>
 
Here is an embeded SVG scene using iframe. Alternatively you could use an "embed" tag (a slightly more complicated solution).
 
'''<nowiki><</nowiki>iframe src="hello-svg.svg" height="200" width="80%" frameborder="0"<nowiki>></nowiki>'''
 
'''    Sorry you need an SVG plugin ...'''
 
'''<nowiki><</nowiki>/iframe<nowiki>></nowiki>'''
 
<nowiki><</nowiki>hr<nowiki>></nowiki> <nowiki><</nowiki>/body<nowiki>></nowiki> <nowiki><</nowiki>/html<nowiki>></nowiki>
 
# The SVG file is a standard stand-alone SVG file
<nowiki><</nowiki>?xml version="1.0" standalone="no"?<nowiki>></nowiki>
 
'''<nowiki><</nowiki>!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"'''
 
'''          "http://www.w3.org/TR/2001/PR-SVG-20010719/DTD/svg10.dtd"<nowiki>></nowiki>'''
 
'''<nowiki><</nowiki>svg xmlns="http://www.w3.org/2000/svg"<nowiki>></nowiki>'''
 
<nowiki><</nowiki>rect x="50" y="50" rx="5" ry="5" width="300" height="100"
 
style="fill:<nowiki> </nowiki>CCCCFF;stroke:<nowiki> </nowiki>000099"/<nowiki>></nowiki>
 
<nowiki><</nowiki>text x="55" y="90" style="stroke:<nowiki> </nowiki>000099;fill:<nowiki> </nowiki>000099;font-size:24;"<nowiki>></nowiki>
 
HELLO dear reader
 
<nowiki><</nowiki>/text<nowiki>></nowiki>
 
<nowiki><</nowiki>/svg<nowiki>></nowiki>
 
----
 
## '''Combining your XML with XHTML'''
 
----
 
# 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'''
 
----


# Tested with Firefox 2.x (works) and IE 7 (CSS doesn<nowiki>’</nowiki>t work, I don<nowiki>’</nowiki>t know why ...)
Procedure:
'''Procedure'''


# Define a namespace for the included XML content
# Define a namespace for the included XML content
Line 701: Line 484:
# Your file must be understood as "XML", e.g. call it something.xml or something.xhtml but not something.html !
# 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)'''
'''XHTML with some cooking XML included (file cooking.xhtml)''':
 
<nowiki><</nowiki>?xml version="1.0" encoding="iso-8859-1"?<nowiki>></nowiki>
<nowiki><</nowiki>?xml version="1.0" encoding="iso-8859-1"?<nowiki>></nowiki>


Line 755: Line 538:
<nowiki><</nowiki>/html<nowiki>></nowiki>
<nowiki><</nowiki>/html<nowiki>></nowiki>


----
#######
----


'''XML with some XHTML included'''


----
;XML with some XHTML included


# Works with Firefox 1x/2x and IE7 (probably also with IE6)
# Works with Firefox 1x/2x and IE7 (probably also with IE6)
Line 769: Line 546:
# Btw this is a trick to get around non-implementation of the xlink standard in IE. If need to create a valid DTD, see  " "  <nowiki>[</nowiki>12<nowiki>]</nowiki>
# Btw this is a trick to get around non-implementation of the xlink standard in IE. If need to create a valid DTD, see  " "  <nowiki>[</nowiki>12<nowiki>]</nowiki>


----


####### '''XML plus XHTML (file xml_plus_xhtml.xml)'''
'''XML plus XHTML (file xml_plus_xhtml.xml):
<nowiki><</nowiki>?xml version="1.0" ?<nowiki>></nowiki>
<nowiki><</nowiki>?xml version="1.0" ?<nowiki>></nowiki>


Line 797: Line 573:


<nowiki><</nowiki>/page<nowiki>></nowiki>
<nowiki><</nowiki>/page<nowiki>></nowiki>
----
#######
----
'''XML Data islands'''
----
# XML DATA islands are a Microsoft/IE extension to HTML
# Based on some early discussion on how to use XML with HTML (from the IE 5 period), <br>data islands are easy to use but break all current HTML standards ...
# You can get similar functionality with other standard technologies, e.g. XSLT style sheets.
'''The principle'''
----
# Usually in the header of the HTML file we define one or more XML data sources within an "xml" tag. These data sources are included either by reference or by cut/paste:
Inclusion by reference to a file (better strategy):
'''<nowiki><</nowiki>xml '''id="cd_info" src="cdlist.xml" /<nowiki>></nowiki>
Inclusion by cut/past (needs to be a full well-formed XML content !)
'''<nowiki><</nowiki>xml<nowiki>>'''<</nowiki>?xml version="1.0" encoding="ISO-8859-1" ?<nowiki>></nowiki> <nowiki><</nowiki>list<nowiki>></nowiki> <nowiki><</nowiki>cd<nowiki>></nowiki> <nowiki><</nowiki>artist<nowiki>></nowiki>The spammers<nowiki><</nowiki>/artist<nowiki>><</nowiki>title<nowiki>></nowiki>Here we go<nowiki><</nowiki>/title<nowiki>></nowiki> <nowiki><</nowiki>price<nowiki>></nowiki>10<nowiki><</nowiki>/price<nowiki>></nowiki> <nowiki><</nowiki>/cd<nowiki>></nowiki> <nowiki><</nowiki>cd<nowiki>></nowiki>  <nowiki><</nowiki>artist<nowiki>></nowiki>The singers<nowiki><</nowiki>/artist<nowiki>><</nowiki>title<nowiki>></nowiki>Let<nowiki>’</nowiki>s hear a song<nowiki><</nowiki>/title<nowiki>><</nowiki>price<nowiki>></nowiki>10<nowiki><</nowiki>/ price<nowiki>><</nowiki>/cd<nowiki>></nowiki> <nowiki><</nowiki>/list<nowiki>></nowiki>
'''<nowiki><</nowiki>/xml<nowiki>></nowiki>'''
----
# The XML file / content needs to well-formed  and you don<nowiki>’</nowiki>t need to worry about namespaces
----
# XML elements can be used to "fill in" various HTML element or attribute contents
----
# See official MS documentation or a textbook for details .......
# In the next slide we just demonstrate one of the most common use cases, i.e. binding HTML table cells contents to XML data.
----
####### '''MS XML data island table example'''
'''XML contents (file cdlist.xml)'''
<nowiki><</nowiki>?xml version="1.0" encoding="ISO-8859-1" ?<nowiki>></nowiki>
<nowiki><</nowiki>list<nowiki>></nowiki>
<nowiki><</nowiki>cd<nowiki>></nowiki>
<nowiki><</nowiki>artist<nowiki>></nowiki>The spammers<nowiki><</nowiki>/artist<nowiki>></nowiki>
<nowiki><</nowiki>title<nowiki>></nowiki>Here we go<nowiki><</nowiki>/title<nowiki>></nowiki>
<nowiki><</nowiki>price<nowiki>></nowiki>10<nowiki><</nowiki>/price<nowiki>></nowiki>
<nowiki><</nowiki>/cd<nowiki>></nowiki>
<nowiki><</nowiki>cd<nowiki>></nowiki>
<nowiki><</nowiki>artist<nowiki>></nowiki>The singers<nowiki><</nowiki>/artist<nowiki>></nowiki>
<nowiki><</nowiki>title<nowiki>></nowiki>Let<nowiki>’</nowiki>s hear a song<nowiki><</nowiki>/title<nowiki>></nowiki>
<nowiki><</nowiki>price<nowiki>></nowiki>10<nowiki><</nowiki>/price<nowiki>></nowiki>
<nowiki><</nowiki>/cd<nowiki>></nowiki>
<nowiki><</nowiki>/list<nowiki>></nowiki>
We would like to display contents of this XML file within a table inside an HTML document
----
# We create a reference to the clist.xml file (see next slide for HTML code !)
'''<nowiki><</nowiki>xml id="cd_info" src="cdlist.xml" /<nowiki>></nowiki>'''
# We then tell the table that we are going to use this data source
'''<nowiki><</nowiki>table datasrc="<nowiki> </nowiki>cd_info" border="1"<nowiki>></nowiki>'''
# In the table body we associate contents of span with data source elements
'''<nowiki><</nowiki>td<nowiki>><</nowiki>span datafld="artist"<nowiki>><</nowiki>/span<nowiki>><</nowiki>/td<nowiki>></nowiki>'''
'''<nowiki><</nowiki>td<nowiki>><</nowiki>span datafld="title"<nowiki>><</nowiki>/span<nowiki>><</nowiki>/td<nowiki>></nowiki>'''
Important: You must use a span element (XML data can not directly be bound to "td" elements !)x
'''HTML (file xml_data_island.html)'''
<nowiki><</nowiki>!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"<nowiki>></nowiki>
<nowiki><</nowiki>html<nowiki>></nowiki>
<nowiki><</nowiki>head<nowiki>></nowiki>
<nowiki><</nowiki>title<nowiki>></nowiki>MS XML Data Islands<nowiki><</nowiki>/title<nowiki>></nowiki>
<nowiki><</nowiki>!-- identification of the data source --<nowiki>></nowiki>
''' <nowiki><</nowiki>xml id="cd_info" src="cdlist.xml" /<nowiki>></nowiki>'''
<nowiki><</nowiki>/head<nowiki>></nowiki>
<nowiki><</nowiki>body<nowiki>></nowiki>
<nowiki><</nowiki>h1<nowiki>></nowiki>MS XML Data Islands<nowiki><</nowiki>/h1<nowiki>></nowiki>
The table below imports data from cdlist.xml
'''<nowiki><</nowiki>table datasrc="<nowiki> </nowiki>cd_info" border="1"<nowiki>></nowiki>'''
<nowiki><</nowiki>thead<nowiki>></nowiki>
:  <nowiki><</nowiki>tr<nowiki>></nowiki> <nowiki><</nowiki>th<nowiki>><</nowiki>span<nowiki>></nowiki>Artist Name<nowiki><</nowiki>/span<nowiki>><</nowiki>/th<nowiki>></nowiki>
:      <nowiki><</nowiki>th<nowiki>><</nowiki>span<nowiki>></nowiki>Title<nowiki><</nowiki>/span<nowiki>><</nowiki>/th<nowiki>></nowiki>
:      <nowiki><</nowiki>th<nowiki>><</nowiki>span<nowiki>></nowiki>Price<nowiki><</nowiki>/span<nowiki>><</nowiki>/th<nowiki>></nowiki>  <nowiki><</nowiki>/tr<nowiki>></nowiki>
<nowiki><</nowiki>/thead<nowiki>></nowiki>
<nowiki><</nowiki>tbody<nowiki>></nowiki>
: <nowiki><</nowiki>tr<nowiki>></nowiki>
:  '''<nowiki><</nowiki>td<nowiki>><</nowiki>span datafld="artist"<nowiki>><</nowiki>/span<nowiki>><</nowiki>/td<nowiki>></nowiki>'''
:'''  <nowiki><</nowiki>td<nowiki>><</nowiki>span datafld="title"<nowiki>><</nowiki>/span<nowiki>><</nowiki>/td<nowiki>></nowiki>'''
:'''  <nowiki><</nowiki>td<nowiki>><</nowiki>span datafld="price"<nowiki>><</nowiki>/span<nowiki>><</nowiki>/td<nowiki>></nowiki>'''
{|border="0" cellspacing="2" width="19%"
|&nbsp;
| <nowiki><</nowiki>/tr<nowiki>></nowiki>
|&nbsp;
|-
|}
<nowiki><</nowiki>/tbody<nowiki>></nowiki>
<nowiki><</nowiki>/table<nowiki>></nowiki>
<nowiki><</nowiki>hr<nowiki>></nowiki> <nowiki><</nowiki>/body<nowiki>></nowiki> <nowiki><</nowiki>/html<nowiki>></nowiki>




[[category:XML]]
[[Category: Technologies]]
[[Category: XML]]

Revision as of 21:00, 20 March 2007

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

The Namespace standard:

  1. Namespaces are identified with URIs
  1. A URI is either a URL or a URN
  1. Compound W3C documents


== Why XML namespaces

ambiguity of an element name

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. 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 declaration variants (1) declaring a namespace that will require insertion of prefixes

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

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

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

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

Scoping

  1. "Scoping" here means "where does a declaration apply ?"
  2. 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
  3. 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.
  • Default the 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 (file link_list.xml)

<?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://www.webster.ch/">Webster Geneva</el>

<el xlink:href="http://www.webster.edu/"

xlink:type="simple">Webster</el>

</link_list>

  1. Note: This file will not display clickable links in IE (since IE does not implement XLinks)
  2. See " " [24] for a workaround using XHTML tags

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

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 / 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 your XML with XHTML

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://webster.unige.ch/coap21/dolores">

<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. Dolores for a COAP 2180 CSS 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>Hilaire Walden</re:recipe_author>
  <re:meal_type>Fish and Shellfish</re:meal_type>

</re:recipe_head>

.....

</re:recipe>

</body>

</html>


XML with some XHTML included
  1. Works with Firefox 1x/2x and IE7 (probably also with IE6)
  2. An HTML namespace is declared in the root element and we use it twice (for the img and a tags).
  3. Btw this is a trick to get around non-implementation of the xlink standard in IE. If need to create a valid DTD, see " " [12]


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="http://tecfa.unige.ch/tecfa-people/ schneider.html">DKS/Tecfa</html:a> , feb 2007 </comment>

</page>