On the Wire


XML
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

1 Using XML and Script to Set Levels of Display

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 transformNode() method of the DOM, in particular, enables you to dynamically apply different XSL stylesheets to the same XML source document, producing different views of the same document at the click of a button.
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/.
Top

2 The transformNode Method

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 transformNode() method.
The syntax of the transformNode() method, which is available in ASP, JScript, Visual Basic, and VBScript, goes like this:
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 strResult, which is a string that contains the results of the XSLT transformation, can then be put into an HTML document by, for example, creating a <div id="results"></div> tag, setting a reference to it, and then using the innerHTML method to insert the string results.
Top

3 Using TransformNode() to Switch XSLT Stylesheets

I've written two basic stylesheets for my source document:
  • Listing 2 displays the first level of nested topics, giving readers a summary of sorts.
  • Listing 3 displays the document's entire contents.
To enable a user with IE5 or higher running at least version 3 of the MSXML parser to select the level of detail of the document to be viewed, you'll first need to use script to load both the XML source file and the default XSLT stylesheet into the MSXML parser. The page will then have to provide users with a means to toggle between the different views while the script, when activated by a button pressed by the user, presents the document after it's transformed by the other stylesheet.
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> tag triggers the script's function and displays the contents of the document using the default stylesheet in Listing 2.
<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 transformPage function without sending a request back to the server:
<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 transformPage function is applied to the XML document and the results are inserted inside the <div id="results"> tag.
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.
Top
Google       

Criticism.com Web

cover artHTML, XHTML, and CSS, Sixth Edition (Visual Quickstart Guide)
Peachpit Press
New $19.79
cover artHead First HTML with CSS & XHTML (Head First)
O'Reilly Media, Inc.
New $25.19
cover artProfessional Ajax, 2nd Edition (Programmer to Programmer)
Wrox
New $26.39
cover artService-Oriented Architecture (SOA): Concepts, Technology, and Design (The Prentice Hall Service-Oriented Computing Series from Thomas Erl)
Prentice Hall PTR
New $35.99
cover artRuby Cookbook (Cookbooks (O'Reilly))
O'Reilly Media, Inc.
New $32.99
cover artHTML & XHTML: The Definitive Guide (6th Edition)
O'Reilly Media, Inc.
New $32.99
cover artFileMaker Pro 8: The Missing Manual
O'Reilly Media, Inc.
New $23.07
cover artFrontPage 2003 (The Missing Manual)
O'Reilly Media, Inc.
New $19.77
cover artProfessional JavaScript for Web Developers (Wrox Professional Guides)
Wrox
New $25.19
cover artCSS Pocket Reference (Pocket Reference (O'Reilly))
O'Reilly Media, Inc.
New $9.95
(Prices May Change)
Privacy Information

cover artHead Rush Ajax (Head First)
O'Reilly Media, Inc.
New $25.19
cover artProfessional ASP.NET 2.0 Special Edition (Wrox Professional Guides)
Wrox
New $37.79
cover artDynamic HTML: The Definitive Reference (Dynamic Html)
O'Reilly Media, Inc.
New $37.79
cover artHTML Utopia: Designing Without Tables Using CSS
SitePoint
New $25.17
cover artGroovy in Action
Manning Publications
New $32.99
cover artPrototype and Scriptaculous in Action
Manning Publications
New $29.69
cover artAjax Design Patterns
O'Reilly Media, Inc.
New $29.69
cover artHTML 4 For Dummies (Html 4 for Dummies)
For Dummies
New $16.49
cover artCascading Style Sheets: The Definitive Guide, 2nd Edition
O'Reilly Media, Inc.
New $39.95
cover artJavaScript & DHTML Cookbook
O'Reilly Media, Inc.
New $26.37
(Prices May Change)
Privacy Information