|C R I T I C I S M . C O M :: Home XML xsl-switch||Search | Site Map | About Criticism.Com|
On the Wire
Switching Document Views
By Steve Hoenisch
Last updated on March 8, 2006
Copyright 1996-2006 www.Criticism.Com
Table of Contents
1 Using XML and Script to Set Levels of Display
2 The transformNode Method
3 Using TransformNode() to Switch XSLT Stylesheets
Complex technical documentation presented on the Internet calls for user interfaces or navigational options that empower readers to quickly gain access to the information that suits their needs. If your readers are viewing documents in an Internet Explorer-only environment, you can let them select the level of detail that will be displayed by combining XML, XSLT, script, and methods of the Document Object Model.
The simple XML document in Listing 1 is structured into a hierarchy of topics with IBM's Darwin Information Typing Architecture (DITA), a DTD for creating modular documentation. (For more on DITA, see www-106.ibm.com/developerworks/xml/.) The document in Listing 1 can be displayed using a single XSLT stylesheet by embedding the xml:stylesheet processing instruction after the XML version processing instruction, as this code shows:
<?xml version="1.0" standalone="yes"?> <?xml:stylesheet type="text/xsl" href= "listing2.xsl"?>
When an XML document with an xml:stylesheet processing instruction is loaded into Internet Explorer 5.0 or higher, it's formatted according to the template rules in the stylesheet. (Because the example stylesheets in this article conform to the W3C's XSLT Recommendation while the MSXML parser with which Internet Explorer 5 is natively equipped does not, you must have installed at least version 3 of the MSXML parser and be running it in replace mode for the code to work. You can obtain the latest version of the MSXML parser at http://msdn.microsoft.com and install it in a few minutes.)
Because the xml:stylesheet processing instruction can apply only one stylesheet to the document, it limits readers to the view prescribed by the stylesheet named listing3.xsl. The reader, however, may prefer to view only a certain level of the document's detail. Some readers, for instance, may want to view only an abstract, others an abridged view, and still others the entire piece. You can use a scripting language that supports the DOM to dynamically apply different stylesheets to an XML source document, thereby allowing readers to switch among various views of the same Web page with the press of a button.
A W3C specification, the DOM is a platform- and language-neutral interface that provides programs and scripts with the means to dynamically access and modify the content, structure, and style of documents, including those in HTML, XHTML, and XML format. For details, see the W3C's DOM page at www.w3.org/DOM/.
It's the DOM's transformNode method that lets you dynamically apply XSLT stylesheets to an XML document. If you create two instances of the DOM's Document object and load the XML source into one of them and the XSL stylesheet into the other, you can then use the stylesheet to transform the XML source by invoking the
The syntax of the
strResult = objXMLNode.transformNode (objStylesheet)
The objXMLNode typically takes a DOM Document object, though it can also take a node and its children within a DOM Document. The objStylesheet is usually a DOM Document instance containing a valid XSL stylesheet. The
I've written two basic stylesheets for my source document:
The script, which is embedded in an HTML page, appears in Listing 4, which can be downloaded at www.sys-con.com/xml/sourcec.cfm. (To see the code work, you must download all four files into the same directory on your hard drive, and then open Listing4.html in IE5.5 or later.) A similar version of this script, replete with debugging logic, is available in Michael Kay's XSLT Programmer's Reference (Wrox), a book that covers XSLT in full and provides an excellent rundown of how to use XML and XSLT with Internet Explorer as well as an insightful discussion of the different versions of the MSXML parser.
After the HTML page in Listing 4 loads, the onLoad attribute of the
<body onLoad="transformPage ('listing2.xsl')">
The HTML page presents the user with two buttons, each of which specifies a different stylesheet to be used as the stylesheet parameter in the script's
<button onClick= "transformPage('listing3.xsl')"> View Entire Document</button> <button onClick="transformPage ('listing2.xsl')">View Summary </button>
When a user selects one of the buttons, the stylesheet specified in the argument of
This is just a sample of what can be done when you combine XML, XSLT, script, and methods of the DOM. If you're displaying complex documentation on the Web, such methods can be expanded to develop potent interfaces that give users the power to quickly find the information they're seeking.
Published June 20, 2002, in XML Journal.