XSLT - On the Client
If your browser supports it, XSLT can be used to transform the document to
XHTML in your browser.
A JavaScript Solution
In the previous chapters we have explained how XSLT can be used to transform a document
from XML to XHTML. We did this by adding an XSL style sheet to the XML
file and let the browser do the transformation.Even if this works fine, it is not always desirable to include a style sheet reference in
an
XML file (e.g. it will not work in a non XSLT aware browser.)
A more versatile solution would be to use a JavaScript to do the transformation.
By using a JavaScript, we can:
- do browser-specific testing
- use different style sheets according to browser and user
needs
That is the beauty of XSLT! One of the design goals for XSLT was to make it
possible to transform data from one format to another, supporting different
browsers and different user needs.
XSLT transformation on the client side is bound to be a major part of the
browsers work tasks in the future, as we will see a growth in the specialized
browser market (Braille, aural browsers, Web printers, handheld devices, etc.)
The XML File and the XSL File
Look at the XML document that you have seen in the previous chapters:
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
.
.
.
</catalog>
|
View the XML file.
And the accompanying XSL style sheet:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title" /></td>
<td><xsl:value-of select="artist" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
|
View the XSL file.
Notice that the XML file does not have a reference to the XSL file.
IMPORTANT: The above sentence indicates that an XML file could be
transformed using many different XSL style sheets.
Transforming XML to XHTML in the Browser
Here is the source code needed to transform the XML file to XHTML on the
client:
<html>
<body>
<script type="text/javascript">
// Load XML
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("cdcatalog.xml")
// Load XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("cdcatalog.xsl")
// Transform
document.write(xml.transformNode(xsl))
</script>
</body>
</html>
|
Tip: If you don't know how to write JavaScript, you can study our
JavaScript tutorial.
The first block of code creates an instance of the Microsoft XML parser
(XMLDOM), and loads the XML file into memory. The second block of code creates
another instance of the parser and loads the XSL file into memory. The last
line of code transforms the XML document using the XSL document, and displays the
result as XHTML in your browser. Nice!
See how it works in IE.
|