18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Chapter 15<br />

You don’t need to start the transformation from the document level; every node has the<br />

transformNode() method. The following are all valid:<br />

sResult = oXmlDom.documentElement.transformNode(oXslDom);<br />

sResult = oXmlDom.documentElement.childNodes[1].transformNode(oXslDom);<br />

sResult = oXmlDom.getElementsByTagName(“name”)[0].transformNode(oXslDom);<br />

sResult = oXmlDom.documentElement.firstChild.lastChild.transformnode(oXslDom);<br />

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

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

that node came.<br />

The more complicated way to use XSLT in IE is to use an XSL template and processor. To do so, you<br />

must use a few more ActiveX controls from the MSXML library. First, the XSLT file must be loaded into<br />

a free-threaded DOM document, which behaves just like a regular DOM document but is thread-safe:<br />

var oXslDom = new ActiveXObject(“MSXML2.FreeThreadedDOMDocument”);<br />

oXslDom.async = false;<br />

oXslDom.load(“employees.xsl”);<br />

After the free-threaded DOM document is created and loaded, it must be assigned to an XSL template,<br />

which is another ActiveX object:<br />

var oTemplate = new ActiveXObject(“MSXML2.XSLTemplate”);<br />

oTemplate.stylesheet = oXslDom;<br />

The XSL template is then used to create an XSL processor (you guessed it, another ActiveX object):<br />

var oProcessor = oTemplate.createProcessor();<br />

With the processor created, you set the input property equal to the XML DOM node to transform and<br />

then call the transform() method:<br />

oProcessor.input = oXmlDom;<br />

oProcessor.transform();<br />

The resulting string is then accessible from the output property:<br />

var sResult = oProcessor.output;<br />

All this code mimics the functionality of transformNode(). You may be wondering why anyone would<br />

use the XSL template/processor methodology if it does the same thing as transformNode(). The answer<br />

is that the processor allows you more control over XSLT.<br />

<strong>For</strong> example, XSLT style sheets accept parameters that can be passed in and used as local variables.<br />

Consider the following style sheet:<br />

<br />

<br />

<br />

474

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

Saved successfully!

Ooh no, something went wrong!