04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 15: XML in JavaScript<br />

using JavaScript, it outputs the employee ’ s position first. This can be done in JavaScript using the<br />

setStartMode() method as shown here:<br />

processor.input = xmldom;<br />

processor.addParameter(“message”, “Hello World!”);<br />

processor.setStartMode(“title-first”);<br />

processor.transform();<br />

The setStartMode() method accepts only one argument, which is the mode to set the processor to. Just<br />

as with addParameter() , this must be called before transform() .<br />

If you are going to do multiple transformations using the same style sheet, you can reset the processor<br />

after each transformation. When you call the reset() method, the input and output properties are<br />

cleared, as well as the start mode and any specified parameters. The syntax for this method is as follows :<br />

processor.reset();<br />

//prepare for another use<br />

Because the processor has compiled the XSLT style sheet, it is faster to make repeat transformations<br />

versus using transformNode() .<br />

MSXML supports only XSLT 1.0. Development on MSXML has stopped since<br />

Microsoft ’ s focus has shifted to the .NET Framework. It is expected that, at some<br />

point in the future, JavaScript will have access to the XML and XSLT .NET objects.<br />

The XSLT Processor Type<br />

Mozilla implemented JavaScript support for XSLT in Firefox by creating a new type. The<br />

XSLTProcessor type allows developers to transform XML documents by using XSLT in a manner<br />

similar to the XSL processor in IE. Since it was first implemented, Chrome, Safari (version 3 and later),<br />

and Opera (version 9 and later) have copied the implementation, making XSLTProcessor into a de facto<br />

standard for JavaScript - enabled XSLT transformations.<br />

As with the IE implementation, the first step is load two DOM documents, one with the XML and the<br />

other with the XSLT. After that, create a new XSLTProcessor and use the importStylesheet()<br />

method to assign the XSLT to it, as shown in this example:<br />

var processor = new XSLTProcessor()<br />

processor.importStylesheet(xsltdom);<br />

The last step is to perform the transformation. This can be done in two different ways. If you want to<br />

return a complete DOM document as the result, call transformToDocument() . You can also get a<br />

document fragment object as the result by calling transformToFragment() . Generally speaking, the only<br />

reason to use transformToFragment() is if you intend to add the results to another DOM document.<br />

When using transformToDocument() , just pass in the XML DOM and use the result as another<br />

completely different DOM. Here’s an example:<br />

var result = processor.transformToDocument(xmldom);<br />

alert(serializeXml(result));<br />

543

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

Saved successfully!

Ooh no, something went wrong!