04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

XSLT Support in Browsers<br />

Chapter 15: XML in JavaScript<br />

XSLT is a companion technology to XML that makes use of XPath to transform one document<br />

representation into another. Unlike XML and XPath, XSLT has no formal API associated with it and is<br />

not represented in the formal DOM at all. This left browser vendors to implement support in their own<br />

way. The first browser to add XSLT processing in JavaScript was IE.<br />

XSLT in Internet Explorer<br />

As with the rest of the XML functionality in IE, XSLT support is provided through the use of ActiveX<br />

objects. Beginning with MSXML 3.0 (shipped with IE 6.0), full XSLT 1.0 support is available via<br />

JavaScript.<br />

Simple XSLT Transformations<br />

The simplest way to transform an XML document using an XSLT style sheet is to load each into a DOM<br />

document and then use the transformNode() method. This method exists on every node in a document<br />

and accepts a single argument, which is the document containing an XSLT style sheet. The<br />

transformNode() method returns a string containing the transformation. Here is an example:<br />

//load the XML and XSLT (IE-specific)<br />

xmldom.load(“employees.xml”);<br />

xsltdom.load(“employees.xslt”);<br />

//transform<br />

var result = xmldom.transformNode(xsltdom);<br />

This example loads a DOM document with XML and a DOM document with the XSLT style sheet. Then,<br />

transformNode() is called on the XML document node, passing in the XSLT. The variable result is<br />

then filled with a string resulting from the transformation. Note that the transformation began at the<br />

document node level because that ’ s where transformNode() was called. XSLT transformations can also<br />

take place anywhere in the document by calling transformNode() on the node at which you want the<br />

transformations to begin. Here is an example:<br />

result = xmldom.documentElement.transformNode(xsltdom);<br />

result = xmldom.documentElement.childNodes[1].transformNode(xsltdom);<br />

result = xmldom.getElementsByTagName(“name”)[0].transformNode(xsltdom);<br />

result = xmldom.documentElement.firstChild.lastChild.transformNode(xsltdom);<br />

If you call transformNode() from anywhere other than the document element, you start the<br />

transformation at that spot. The XSLT style sheet, however, still has access to the full XML document<br />

from which that node came.<br />

Complex XSLT Transformations<br />

The transformNode() method gives basic XSLT transformation capabilities, but there are more<br />

complex ways to use the language. To do so, you must use an XSL template and an XSL processor. The<br />

first step is to load the XSLT style sheet into a thread - safe version of an XML document. This is done by<br />

539

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!