21.4.2 Evaluating XPath Expressions
Example 21-10 shows an
XML.XPathExpression class that works in IE and in standards-compliant browsers such as Firefox.
Example 21-10. Evaluating XPath expressions
21.4.3 More on the W3C XPath API
Because of the limitations in the IE XPath API, the code in Example 21-10 handles only queries that evaluate to a document node or set of nodes. It is not possible in IE to evaluate an XPath expression that returns a string of text or a number. This is possible with the W3C standard API, however, using code that looks like this:
There are two things to note about these simple examples. First, they use the
document.evaluate() method to evaluate an XPath expression directly without compiling it first. The code in Example 21-10 instead used
document.createExpression() to compile an XPath expression into a form that could be reused. Second, notice that these examples are working with HTML
<p> tags in the document object. In Firefox, XPath queries can be used on HTML documents as well as XML documents. See Document, XPathExpression, and XPathResult in Part IV for complete details on the W3C XPath API.