We support a simplified xpath syntax. The path contains 1 or more of the following, separated by '/', such as:
para children named 'para' para[5] just the 5th child named 'para' (1-based) * all element children text() all text node childrenThe beginning of a path can be:
nothing, just start with the provided context node . same as nothing (start with context node), but useful to prefix // / start from document and ignore context node // look for the next path segment to begin any number of levels below context nodeIn the middle of the path there can be:
// go to all descendantsNot supported:
For other attempts at simplifying XPath for various purposes, see for example:
Starting with MSXML 2 (IE5.5+), IE has Node.selectNodes(expr) and Node.selectSingleNode(expr) which can be used on XML documents for example from XMLDOM or XMLHTTP. There are installation/version complexities as to whether the expression language is true XSLT or not; generally speaking, MSXML2 (IE5.5) is Microsoft's language, and MSXML3 (IE6) is standard.
Mozilla implements part of DOM Level 2 Traversal: http://www.w3.org/TR/DOM-Level-2-Traversal-Range/traversal.html It implements TreeWalker and NodeFilter, but no NodeIterator so far.
In .NET, Microsoft there is a full XPath implementation including a family of interfaces such as XPathNodeIterator, XPathNavigator, XPathDocument, XPathExpression. (Note that XPathDocument (XPathDocumentIterator) and XmlDocument (DocumentXPathNavigator) both implement IXPathNavigable but XPathDocument has a more efficient implementation for XPath queries. Also re-using XPathNodeIterator instances avoids redundant expression compilation.) An example is:
XPathNavigator nav = new XPathDocument(file).CreateNavigator(); XPathExpression expr = nav.Compile('//bar[@name="id17"]'); XPathNodeIterator nodes = nav.Select(expr);In Mozilla (including Netscape 7) there is an implementation of the W3 DOM Level 3 XPath: http://www.w3.org/TR/DOM-Level-3-XPath/ This module has among other functions:
doc.evaluate(DOMString expression, Node contextNode, XPathNSResolver resolver, unsigned short type, DOMObject resultexpr)
This can be used if you have a DomDocument (from XMLHttpRequest responseXML or createDocument). Other interfaces include XPathException, XPathEvaluator, XPathExpression, XPathNSResolver, XPathResult, XPathNamespace. An example is:var some_bar_node = xmlDocument.evaluate('//bar[@name="id17"]', xmlDocument, null, 9, null).singleNodeValue; var idval = some_bar_node.getAttribute("name"); // idval should now be == 'id17'
Public Member Functions | |
XPath (String pathstr, String flags) | |
A function to return a burst.xml.XPath object. | |
void | iterate (Node node, Function func) |
Call the function for each match to the path, using the provided node as the context node. | |
Node | selectSingleNode (Node node) |
Returns the first matching node, or undefined if none. | |
Array | selectNodes (Node node) |
Collect all matches and return them as an Array. |
|
A function to return a burst.xml.XPath object. Internally it can be used as a constructor, but it should be called as a function so that we can cache compiled XPath instances based on the arguments. The flags supported are:
Note that we assume that the HTML DOM tagName and nodeName always return uppercase; we leave their return values unchanged, and just uppercase our strings before comparing. If flags are not provided (undefined), we default to 'h'.
|
|
Call the function for each match to the path, using the provided node as the context node. If the function returns true, iteration stops. If the path is absolute, then the node argument is irrelevant and can be null.
|
|
Collect all matches and return them as an Array. See selectNodes: http://msdn.microsoft.com/library/en-us/xmlsdk30/htm/xmmthselectnodes.asp Unlike Microsoft selectNodes, our result is not a "live" collection, but a static snapshot.
|
|
Returns the first matching node, or undefined if none. See http://msdn.microsoft.com/library/en-us/xmlsdk30/htm/xmmthselectsinglenode.asp
|