views:

262

answers:

1

From the JavaDoc: returns: The String that is the result of evaluating the expression and converting the result to a String.

/**
 * ...
 * ...
 * @return The <code>String</code> that is the result of evaluating the expression and converting the result to a 
 *   <code>String</code>.
 * ...
 * ...
 */
String javax.xml.xpath.XPathExpression.evaluate(Object item)

The question is, it is a bit vauge what is the contract here in case the expression finds nothing. Is null an valid / invalid return in all implementations? where is the return API defined? in the JSR?

+2  A: 

If I remember correctly, there is no such thing as null in XPath. My guess would be it returns the empty string.

Update: a quick look at XPath 2.0 and XPath 2.0 Functions specs confirms this feeling.

Jerome
Makes sense, but is it documented somewhere? is there an official guidline to the implementers of this API to avoid returning null? and is there a link to it?And if so, then how would one differ between "your xpath found a node, and it contained the value 'empty string'" to "your xpath didn't find anything"? (I guess that for this you have the evaluate that returns an object)
Ehrann Mehdan
If you don't find anything, the XPath returns the empty sequence, which is converted to the empty string (http://www.w3.org/TR/xquery-operators/#func-string)
Jerome
javax.xml.xpath.XPathExpression.evaluate(Object item, QName returnType) returns null if it doesn't find anything by the way
Ehrann Mehdan