Skip to main content

question on xpath expression types

1 reply [Last post]
Joined: 2008-03-27

In the XPath class (1.6), I can't find a way to get the result type from the XPathExpression.

The XPath evaluate() methods either return a string or take the result type as a parameter:
Object evaluate(string expression, InputSource source, QName returnType) {..}

I can also compile the XPath expression for later evaluation:
XPathExpression compile(String expression)

But I don't also see how to get the result type from the XPathExpression.

Say I have a compiled XPath expression passed in a XPathExpression parameter to an eval(XPathExpression expr) method. In expr() I want to know whether that expression returns a boolean, a number (eg the count() xpath function) or a NodeList. But instead of being able to get that info, I have to tell it what type I want. How do I know, in the general case? If the expression invokes count(), I know it's Number. But I may not know
what the expression is doing: it's an already compiled parameter.

Is there something I'm not seeing here?

Is there a way to get the result type from the XPathExpression, or XPath?

I also don't see any specification of what happens when the result type I request is say NodeList and the result (e.g. for count()) is Number. Is it silently converted to a Node in a NodeList, or does this throw an exception? Thanks.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2008-03-27

I've been playing around with this and if the evaluate() throws an exception on returning
NODE or NODESET, I can evaluate() as a String and parse the contents to determine if it is
really a BOOLEAN or really a NUMBER.

This is acceptable, barely. It involves the performance penalty of doing extra evaluates,
but at least I can get the info.

This XPathExpression return type info is known at XPath compile time, it seems to me it ought to be preserved in the XPathExpression.