This chapter mainly explains the following four questions:
1. Parsing XML into DOM documents
XML is parsed into DOM documents across browsers:
function parseXml(xml){ var xmldom = null; if (typeof DOMParser != "undefined"){ xmldom = (new DOMParser()).parseFromString(xml, "text/xml"); var errors = xmldom.getElementsByTagName("parsererror"); if (errors.length){ throw new Error("XML parsing error:" + errors[0].textContent); } } else if (typeof ActiveXObject != "undefined"){ xmldom = createDocument(); xmldom.loadXML(xml); if (xmldom.parseError != 0){ throw new Error("XML parsing error: " + xmldom.parseError.reason); } } else { throw new Error("No XML parser available."); } return xmldom; }
This parseXml() function takes only one parameter and parses the XML string.
2. Serializing DOM documents into XML documents
Serialize DOM documents into XML documents across browsers:
function serializeXml(xmldom){ if (typeof XMLSerializer != "undefined"){ return (new XMLSerializer()).serializeToString(xmldom); } else if (typeof xmldom.xml != "undefined"){ return xmldom.xml; } else { throw new Error("Could not serialize XML DOM."); } }
This serializeXml() function takes a parameter, the XML DOM document to be serialized.
3,XPath
Cross-browser XPath:
Recreate the selectSingleNode() and selectNodes() methods. The function takes three parameters: context nodes, XPath expressions, and optional namespace objects. Namespace objects should be in the form of the following literal quantities.
{ prefix1: "uri1", prefix2: "uri2", prefix3: "uri3" }
function selectSingleNode(context, expression, namespaces){ var doc = (context.nodeType != 9 ? context.ownerDocument : context); if (typeof doc.evaluate != "undefined"){ var nsresolver = null; if (namespaces instanceof Object){ nsresolver = function(prefix){ return namespaces[prefix]; }; } var result = doc.evaluate(expression, context, nsresolver, XPathResult.FIRST_ORDERED_NODE_TYPE,null); return (result !== null ? result.singleNodeValue : null); } else if (typeof context.selectSingleNode != "undefined"){ //create namespace string if (namespaces instanceof Object){ var ns = ""; for (var prefix in namespaces){ if (namespaces.hasOwnProperty(prefix)){ ns += "xmlns:" + prefix + "='" + namespaces[prefix] + "' "; } } doc.setProperty("SelectionNamespaces", ns); } return context.selectSingleNode(expression); } else { throw new Error("No XPath engine found."); } } var result = selectSingleNode(xmldom.documentElement, "wrox:book/wrox:author", { wrox: "http://www.wrox.com/" }); alert(serializeXml(result));
function selectNodes(context, expression, namespaces){ var doc = (context.nodeType != 9 ? context.ownerDocument : context); if (typeof doc.evaluate != "undefined"){ var nsresolver = null; if (namespaces instanceof Object){ nsresolver = function(prefix){ return namespaces[prefix]; }; } var result = doc.evaluate(expression, context, nsresolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); var nodes = new Array(); if (result !== null){ for (var i=0, len=result.snapshotLength; i < len; i++){ nodes.push(result.snapshotItem(i)); } } return nodes; } else if (typeof context.selectNodes != "undefined"){ //create namespace string if (namespaces instanceof Object){ var ns = ""; for (var prefix in namespaces){ if (namespaces.hasOwnProperty(prefix)){ ns += "xmlns:" + prefix + "='" + namespaces[prefix] + "' "; } } doc.setProperty("SelectionNamespaces", ns); } var result = context.selectNodes(expression); var nodes = new Array(); for (var i=0,len=result.length; i < len; i++){ nodes.push(result[i]); } return nodes; } else { throw new Error("No XPath engine found."); } } var result = selectNodes(xmldom.documentElement, "wrox:book/wrox:author", { wrox: "http://www.wrox.com/" }); alert(result.length);
4. Using XSLT Style Sheets to Convert XML Documents
XSLT is used across browsers:
function transform(context, xslt){ if (typeof XSLTProcessor != "undefined"){ var processor = new XSLTProcessor(); processor.importStylesheet(xslt); var result = processor.transformToDocument(context); return (new XMLSerializer()).serializeToString(result); } else if (typeof context.transformNode != "undefined") { return context.transformNode(xslt); } else { throw new Error("No XSLT processor available."); } }
This transform() function takes two parameters: the context node to perform the transformation and the XSLT document object.