How to display TEXT by a browser?
Write TEXT into HTML, better: XHTML
Just that?
better: use CSS
How to display XML by a browser?
Transform XML into XHTML
How to do?
use XSL.
XSL stands for EXtensible Stylesheet Language.
HTML uses CSS as a style sheets; XML uses XSL instead.
XSL is the XML Style Sheets. It contains XSLT for
transforming XML documents, XPath for navigating in
XML documents, and XSL-FO for formatting XML documents.
XSLT transforms an XML document into another XML document
recognized by a browser: Transformation here does
more than what It does a CSS sheet styles. XSML transforms
a source XML document into HTML and XHTML (by transforming each
XML element). It allows elements and attributes to be added
or removed from the output XML file. We have also the possiilities
to rearrange and sort elements, and more.
Currently, the major browsers are available with an XSLT
implementation.
2. XML document:
We consider the following XML document. Example:
a. Saved as atoms.txt (the raw XML source document)
a href = "atoms.txt" atoms.txt.
"View Source" from the browser menu will display the
same style document.
b. Saved as atoms.xml, without XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>
a href = "atoms0.xml" atoms.xml
The plus (+) and minus sign (-) to the left of the
elements can be clicked to expand or collapse the
structure of the element.
b. Saved as atoms.xml, including XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="atoms.xsl"?>
3. XSL Document:
An XSL document contains:
3.0. <?xml version="1.0" encoding="ISO-8859-1"?>
3.1. A root element: Style Sheet Declaration
writen at
the top of the document, according to W3C namespace,
like this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
(use stylesheet or transform: they are the same)
3.2 Elements:
3.2.1. Template elements <xsl:template>
It contains rules for nodes. Its attribute "match" is used to
associate this template to the XML element, or to the
whole XML document (specified in XPath by "/"). Example: View the output
3.2.2. <xsl:value-of> Element
The <xsl:value-of> element is used to extract the value of a selected node.
The <xsl:value-of> Element
This element is used to extract values from an XML
element and add them to be displayed in the output
stream of the transformation with an XHTML document. Example:
In atoms.xml file, we can then extract the following:
<tr>
<td><xsl:value-of select="ATOMS/NOBLEGAS/NAME"/></td>
<td><xsl:value-of select="ATOMS/NOBLEGAS/SYMBOL"/></td>
<td><xsl:value-of select="ATOMS/NOBLEGAS/CHARGE"/></td>
<td><xsl:value-of select="ATOMS/NOBLEGAS/MASS"/></td>
</tr>
Remark:
Inside an <xsl:for-each> element, we can make some selections
like filtring by not to display the Xenon noble gas in the atoms.xml:
<xsl:for-each select="ATOMS/NOBLEGAS/SYMBOL
[SYMBOL !='Xe']">
...
3.2.5. <xsl:if> Element
Its syntax is:
<xsl:if test ="expression">
Do something ...
</xsl:if>
Its place is inside the <xsl:for-each> element
in the XSL file:
<xsl:for-each select="ATOMS/NOBLEGAS">
<xsl:if test ="MASS < 20">
<tr>
<td><xsl:value-of select="NAME"/></td>
<td><xsl:value-of select="SYMBOL"/></td>
<td><xsl:value-of select="CHARGE"/></td>
<td><xsl:value-of select="MASS"/></td>
</tr>
</xsl:if>
</xsl:for-each>
3.2.6. <xsl:choose> Element
The element choose warps two other elements:
<xsl:when> and <xsl:otherwise>.
The <xsl:choose> element is used in conjunction
with <xsl:when>. It expresses multiple conditional tests.
It resembles to " if () else{}".
Here is an Example:
<xsl:for-each select="ATOMS/NOBLEGAS">
<tr>
<td><xsl:value-of select="NAME"/></td>
<xsl:choose>
<xsl:when test="MASS < 80">
<td bgcolor="#ccffcc">
<xsl:value-of select="NAME"/></td>
</xsl:when>