JavaScript and XML : Part 2-Page 4 | JavaScript: The Definitive Guide, Fifth Edition
[previous] [next]
JavaScript and XML: Part 2
21.5 Serializing XML
It is sometimes useful to serialize an XML document (or some subelement of the document) by converting it to a string. One reason you might do this is to send an XML document as the body of an HTTP POST request generated with the XMLHttpRequest object. Another common reason to serialize XML documents and elements is for use in debugging messages!
In Mozilla-based browsers, serialization is done with an XMLSerializer
object. In IE, it is even easier: the xml property of an XML Document or Element object returns the serialized form of the document or element.
Example 21-11 shows serialization code that works in Mozilla and IE.
Example 21-11. Serializing XML
21.6 Expanding HTML Templates with XML Data
One key feature of IE's XML data islands is that they can be used with an automatic templating facility in which data from a data island is automatically inserted into HTML elements. These HTML templates are defined in IE by adding datasrc
and datafld
("fld" is short for "field") attributes to the elements.
This section applies the XML techniques seen earlier in the chapter and uses XPath and the DOM to create an improved templating facility that works in IE and Firefox. A template is any HTML element with a datasource attribute. The value of this attribute should be the ID of an XML data island or the URL of an external XML document. The template element should also have a foreach
attribute. The value of this attribute is an XPath expression that evaluates to the list of nodes from which XML data will be extracted. For each XML node returned by the foreach expression, an expanded copy of the template is inserted into the HTML document. The template is expanded by finding all elements within it that have adata attribute. This attribute is another XPath expression to be evaluated in the context of a node returned by the foreach expression. This data expression is evaluated with XML.getNode()
, and the text contained by the returned node is used as the content of the HTML element on which the data attribute was defined.
This description becomes much clearer with a concrete example. Example 21-12 is a simple HTML document that includes an XML data island and a template that uses it. It has an onload()
event handler that expands the template.
Example 21-12. An XML data island and HTML template
A critical piece of Example 21-12 is the onload event handler, which calls a function named XML.expandTemplates()
. Example 21-13 shows the implementation of this function. The code is fairly platform-independent, relying on basic Level 1 DOM functionality and on the XPath utility functions XML.getNode()
and XML.getNodes()
defined in Example 21-10.
Example 21-13. Expanding HTML templates
This excerpt is taken from Chapter 21 of JavaScript: The Definitive Guide, Fifth Edition, published by O'Reilly Media, Inc., Copyright © 2006, 2002, 1998, 1997, 1996 O'Reilly Media, Inc. All rights reserved.
[previous] [next]
URL: