Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
The document identified by the stylesheet property must be a free-threaded document object.
Putting it Together
The example in this section shows one way of controlling a transformation using MSXML from within JavaScript on an HTML page.
Example: Using Client-Side JScript to Transform a Document
This example demonstrates the way that you can load, parse, and transform an XML document using client-side JScript in Internet Explorer. You can run this simply by loading the page
default.html
using the IE browser (version 5 or higher). When you first load it, you may see a security warning, depending on your browser security settings. If this happens, right-click on the message and select “Allow blocked content”.
The example shows an HTML page with two buttons on it. The user can click on either of the buttons to select how the data should be displayed. The effect of clicking either button is to apply the corresponding stylesheet to the source XML document.
XML Source
The XML source file for this example is
tables
_
data.xml
. It defines several tables (real tables, the kind you sit at to have your dinner), each looking like this:
…
Stylesheet
There are two stylesheet files,
tables
_
list.xsl
and
tables
_
catalog.xsl
. Since this example is designed to show the JScript used to control the transformation rather than the XSLT transformation code itself, I won't list them here.
HTML page
The page
default.htm
contains some simple styling information for the HTML page, then the JScript code that loads the XML and XSL documents, checks for errors, and performs the transformation. Notice that the
transformFiles
function takes the name of a stylesheet as a parameter, which allows you to specify the stylesheet you want to use at runtime:
The remainder of the file is the HTML that creates the visible part of the page. The opening
element specifies an
onload
attribute that causes the
transformFiles()
function in our script section to run once the page has finished loading:
…
Transforming an XML Document using the client-side code
…
Because it uses the value
tables_list.xsl
for the parameter to the function, this stylesheet is used for the initial display. This shows the data in tabular form.
The next thing in the page is the code that creates the two HTML
elements, marked
Catalog
and
Simple List
. The
onclick
attributes of each one simply execute the
transformFiles()
function again, each time specifying the appropriate stylesheet name:
…
View the tables as a
(‘tables_catalog.xsl’)”>Catalog
or as a
Finally, at the end of the code, you can see the definition of the
Output
When the page is first displayed, it looks like
Figure D-1
.
Click the
Catalog
button, and you will see an alternative graphical presentation of the same data, achieved by applying the other stylesheet.
Restrictions
Microsoft claims full compliance with XSLT 1.0 and XPath 1.0, although there are one or two gray areas where its interpretation of the specification may cause stylesheets to be less than 100% portable. These include: