Evaluate() returns the XPathResult-object. The evaluate()-method has five parameters, but concentrate on the first and fourth parameter and treat the rest as fixed. Here’s an example:
var XPathResult = document.evaluate( 'count(//div)', document, null, XPathResult.ANY_TYPE, null);
If you choose the generic XPathResult.ANY_TYPE, the XPath-expression: count(//div) itself determines the return type. So in this example the return type is XPathResult.NUMBER_TYPE
The return type is important for the further processing of the value. If you like to alert the string-value of a nodeset, then you need to choose XPathResult.STRING_TYPE:
Go to www.it-kosmopolit.de and press F12 to open developer tools. In the Console enter:
var XPathResult = document.evaluate( './/h1', document, null, XPathResult.STRING_TYPE, null); alert(XPathResult.stringValue);
var XPathResult = document.evaluate( 'count(//div)', document, null, XPathResult.ANY_TYPE, null); alert(XPathResult.numberValue);
Unfortunately this function is not implemented in Internet Explorer until now.