[jdom-interest] XPath bindings?

Elliotte Rusty Harold elharo at metalab.unc.edu
Fri Oct 12 06:36:56 PDT 2001

At 11:32 PM -0400 10/11/01, bob mcwhirter wrote:
>Howdy folks--
>Now that Jaxen is pretty darn compliant with regards JDOM (including
>the namespace:: axis), I was wondering if we might want to spec out an
>XPath binding.  I know the FAQ says maybe 1.1 would include XPath support,
>and the JSR is for 1.0 only, but, XPath is seeming very useful to many
>folks, and The Other Models seem to support it.
>Thus, I propose adding, to Document and Element the following:
>	public List selectNodes(String xpathExpr);
>	public Object selectSingleNode(String xpathExpr);

I like the first one. I object to the second. An XPath expression often returns more than one node. What does this method do if the argument in fact returns more than one node? For that matter, what does it do if the XPath selects no nodes at all? In the context of JDOM, an XPath expression should always return a List.

Actually, now that I think about it, the first method's problematic too. An XPath expression may not return a list of nodes. It can return a boolean, a number, a string (which in XPath is NOT the same thing as a text node. Can JDOM even handle this?) or a result tree fragment. How do we handle these cases?

Furthermore, what happens if the XPath expression is illegal? Is an exception thrown? If so what exception? 

I'm -1 on the current proposal. I think if these questions could be answered satisfactorily, then functionality similar to this would be useful. However, please don't rush into adding this until we can have full consideration of the various cases and implications of our decisions. 

>Jaxen supports other convenience methods, such as booleanValueOf(...),
>but I'm not proposing adding those.  Following the 80/20 rule, the
>two simple select..() methods seem sufficient. 

One method I do think we could add to all the core classes (Element, Document, Attribute, etc.) in a very straight-forward way would be valueOf():

public String valueOf();

This would return the string value of the object as defined by XPath. I think this is a very useful thing to have. I also think it would answer a lot of the questions that keep coming up again and again about whether getChildText() and similar methods should trim or normalize or recurse and so forth. XPath has already answered these questions, and we can answer them too just by reference to XPath. 

| Elliotte Rusty Harold | elharo at metalab.unc.edu | Writer/Programmer |
|          The XML Bible, 2nd Edition (Hungry Minds, 2001)           |
|              http://www.ibiblio.org/xml/books/bible2/              |
|   http://www.amazon.com/exec/obidos/ISBN=0764547607/cafeaulaitA/   |
|  Read Cafe au Lait for Java News:  http://www.cafeaulait.org/      | 
|  Read Cafe con Leche for XML News: http://www.ibiblio.org/xml/     |

More information about the jdom-interest mailing list