[jdom-interest] org.jdom.xpath.XPath

Laurent Bihanic laurent.bihanic at atosorigin.com
Wed Apr 10 05:04:21 PDT 2002


As Jason requested some feedback, here it goes:

Although I wrote most of org.jdom.xpath.XPath, I do not agree with the 
interface of the valueOf() method.
The current interface directly exposes the bahaviour of Jaxen which returns an 
empty string in many "error" cases. Although this may be a desirable behaviour 
for XSLT processor, I don't think it applies to an API.

More specifically, valueOf() currently returns an empty string if the XPath 
expression does not match any node. Thus, valueOf() for the XPath expression 
"@name" will return "" if the attribute "name" is missing or if it's actual 
value is "".
The method numberValueOf() already distinguishes the two cases returning 
either null (no match) or Double.NaN (found value cannot be converted into a 
number).

As a programmer I'd like to be able to distinguish between the above two cases 
by simply testing the returned value. Otherwise, I'll have to use a 
combination of XPath.selectSingleNode() and a JDOM method depending of the 
type of object returned.

Thus I'd propose to change the interface of valueOf as follows:

     * @return the string value of the first node selected by applying
     *         the wrapped XPath expression to the given context or
     *         <code>null</code> if no node was selected.

Opinions ?

Laurent


Jason Hunter wrote:
> I just checked in an org.jdom.xpath.XPath class written by Laurent
> Bihanic which provides a simple API for the common XPath calls you'll
> want to make.  It by default right now wraps the Jaxen library, but it's
> designed so we can plug alternative implementations in the future.  I
> welcome people using XPath to test it out and see how it works for you.
> 
> Also, for the people helping design JDOM, the API isn't fixed and we
> should ensure it's exactly what we want.  Bob, your comments are most
> welcome.
> 




More information about the jdom-interest mailing list