If you can rely on id attributes, document.getElementById() is still powerful, but it's not nearly as powerful as XPath. Although walking the DOM tree can achieve similar results, using XPath expressions is much quicker and more powerful. BaseX is a native and light-weight XML database management system and XQuery processor. As opposed to traditional databases, XML documents are portable and can be opened on any platform using any programming language. The XML format is based on opening and closing tags for enclosing data content. You can now "query" the document with XPath expressions. XML is defined as a textual structured format for storing, representing and communicating data. Sample usageĪssume we have the following XML document (see also How to Create a DOM tree and Parsing and serializing XML): Example: An XML document to use with the custom evaluateXPath() utility function If you are using XMLHttpRequest to read a local or remote XML file into a DOM tree (as described in Parsing and serializing XML), the first argument to evaluateXPath() should be req.responseXML. The snippets are functions you can use in the real world in your own code. Otherwise, you have to provide your own implementation of XPathNSResolver. This article provides some XPath code snippets simple examples of how to a few simple utility functions based on standard interfaces from the DOM Level 3 XPath specification that expose XPath functionality to JavaScript code. Without a static representation of XPath: Using static classes to represent XPath queries: Both of these methods work and perform exactly the same. Note that createNSResolver should only be used if you are sure the namespace prefixes in the XPath expression match those in the document you want to query (and that no default namespace is being used (though see document.createNSResolver for a workaround)). function evaluateXPath ( aNode, aExpr ) Evaluate an XPath expression aExpression against a given DOM node // or Document object (aNode), returning the results as an array // thanks wanderingstan at morethanwarm dot mail dot com for the // initial work.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |