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.

Chapter 15: XML in JavaScript<br />

Using the IE transformNode() method ensures that you don ’ t need to use a thread - safe DOM<br />

document for the transformation.<br />

Note that due to different XSLT engines in browsers, the results you receive from a<br />

transformation may vary slightly or greatly from browser to browser. You should<br />

never depend on an absolute transformation result using XSLT in JavaScript.<br />

Summary<br />

There is a great deal of support for XML and related technologies in JavaScript. Unfortunately, because<br />

of an early lack of specifications, there are several different implementations for common functionality.<br />

DOM Level 2 provides an API for creating empty XML documents but not for parsing or serialization.<br />

Because of this lack of functionality, browser vendors began creating their own approaches. IE took the<br />

following approach:<br />

❑<br />

❑<br />

❑<br />

IE introduced XML support through ActiveX objects, the same objects that could be used to<br />

build desktop applications.<br />

The MSXML library ships with Windows and is accessible from JavaScript.<br />

This library includes support for basic XML parsing and serialization as well as complementary<br />

technologies such as XPath and XSLT.<br />

Firefox, on the other hand, implemented two new types to deal with XML parsing and serialization as<br />

follows :<br />

❑<br />

❑<br />

The DOMParser type is a simple object that parses an XML string into a DOM document.<br />

The XMLSerializer type performs the opposite operation, serializing a DOM document into<br />

an XML string.<br />

Due to the simplicity and popularity of these objects, Opera, Chrome, and Safari duplicated the<br />

functionality, and these types are de facto standards in web development.<br />

DOM Level 3 introduced a specification for an XPath API that has been implemented by Firefox, Safari,<br />

Chrome, and Opera. The API enables JavaScript to run any XPath query against a DOM document and<br />

retrieve the result regardless of its data type. IE implemented its own XPath support in the form of two<br />

methods: selectSingleNode() and selectNodes() . Although much more limited than the DOM<br />

Level 3 API, these methods provide basic XPath functionality to locate a node or set of nodes in a DOM<br />

document.<br />

The last related technology is XSLT, which has no public specification defining an API for its usage.<br />

Firefox created the XSLTProcessor type to handle transformations via JavaScript and was soon copied<br />

by Safari, Chrome, and Opera. IE implemented its own solution, both with the simple<br />

transformNode() method and through a more complicated template/processor approach.<br />

XML is now well supported in IE, Firefox, Chrome, Safari, and Opera. Even though the implementations<br />

vary wildly between IE and the other browsers, there ’ s enough commonality to create reasonable<br />

cross - browser functionality.<br />

546

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

Saved successfully!

Ooh no, something went wrong!