MSXML, concluded - MSXML and XSLT
(Page 4 of 5 )
MSXML can be used to transform an XML document using XSLT (see Chapter 6). Many times you’ll want to transform an XML document to an HTML page so a browser can display it. We’ll show you how to do this with MSXML. Here’s the table row in the HTML page that contains the XSLT stylesheet:
<tr valign="top">
<td nowrap><a href="#" onclick="TransformDocument(document.all(
'stylesheet').value); return false;">Transform Document:</a></td>
<td>
<textarea id="stylesheet" rows="20" cols="80">
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/">
<html>
<body>
<h2>CD Listing</h2>
<table border="1">
<tr>
<th align="center">UPC</th>
<th align="center">Artist</th>
<th align="center">Title</th>
</tr>
<xsl:for-each select="catalog/cd">
tr>
<td>
<xsl:value-of select="@upc"/>
</td>
<td>
<xsl:value-of select="artist"/>
</td>
<td>
<xsl:value-of select="title"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
</textarea>
</td>
</tr>
The second table cell has a text area that contains the stylesheet. We’ve provided a default stylesheet, but you can change the default in the browser when you’re running this example. The first cell takes the stylesheet from the text area and passes it as an argument to the TransformDocument() function. Here’s the TransformDocument() function:
function TransformDocument(stylesheet)
{
var xslProcessor;
var xslTemplate = new ActiveXObject("Msxml2.XSLTemplate.4.0");
var xslDocument = new ActiveXObject(
"Msxml2.FreeThreadedDOMDocument.4.0");
xslDocument.async = false;
xslDocument.loadXML(stylesheet);
if (xslDocument.parseError.errorCode != 0)
{
var myErr = xmlDoc.parseError;
alert("You have error " + myErr.reason);
return;
}
xslTemplate.stylesheet = xslDocument;
xslProcessor =
xslTemplate.createProcessor();
xslProcessor.input = objXML;
xslProcessor.transform();
window.frames.htmlresult.document.open();
window.frames.htmlresult.document.clear();
window.frames.htmlresult.document.write(xslProcessor.output);
window.frames.htmlresult.close();
}
The first line declares a variable.
The second line creates an XSLTemplate object and assigns it to a variable.
The third line creates a DOMDocument object and assigns it to a variable.
The fourth line sets the async property to false so the next statement doesn’t execute until the document is loaded.
The fifth line calls the loadXML() method and passes it the stylesheet.
The sixth line determines if there is an error. If so, then an error message is displayed and the function returns to the statement that called it without transforming the XML document.
The seventh line executes if there isn’t an error. This line assigns the xslDocument to the stylesheet property of the xslTemplate.
The eighth line calls the createProcessor() method to create an xslProcessor.
The ninth line assigns the XML document to the input property of the xslProcessor.
The tenth line calls the transform() method to transform the XML document.
Lines 11 through 14 write the transformed XML document to the browser. The results are shown next.
CD Listing
Here is the list of CDs organized by UPC, artist, and title that is produced by using MXSML to transform an XML document using XSLT. This is illustrated in the previous section of this chapter.
UPC | Artist | Title |
602498678299 | U2 | How to Dismantle an Atomic Bomb |
75679244222 | Led Zeppelin | Physical Graffiti |
75678367229 | Rush | Rush in Rio |
74646938720 | Billy Joel | Songs in the Attic |
75678263927 | Led Zeppelin | Houses of the Holy |
8811160227 | Jimi Hendrix | Are You Experienced? |
74640890529 | Bob Dylan | The Times They Are A-Changin’ |
Next: Summary >>
More XML Articles
More By McGraw-Hill/Osborne
|
This article is excerpted from chapter 10 of XML DeMYSTified, written by Jim Keogh and Ken Davidson (McGraw-Hill/Osborne, 2005; ISBN: 0072262109). Check it out today at your favorite bookstore. Buy this book now.
|
|