Xalan XPathAPI question

In our application's code, we need to use XPath expressions to fetch
data from XML documents. I'm using the Apache Xalan XPath implementation
to do this.
The only thing I've not figured out is how to resolve variables. How
can I set an XPath variable to some value from the Java code?

For example:
XML code:
<document>
<element name="foo">This is an element</element>
<element name="bar">This is also an element</element>
</document>

This kind of code, as is, throws a RuntimeException about an
unresolved variable "var". Of course it's unresolved, because I don't
know how to resolve it!
The part that says "//what to write here???" should set the
variable "var" to "foo" so that the node iterator would find this
node:
This is an element

Advertisements

Joona I Palaste wrote:
> In our application's code, we need to use XPath expressions to fetch
> data from XML documents. I'm using the Apache Xalan XPath implementation
> to do this.
> The only thing I've not figured out is how to resolve variables. How
> can I set an XPath variable to some value from the Java code?
>
> For example:
> XML code:
> <document>
> <element name="foo">This is an element</element>
> <element name="bar">This is also an element</element>
> </document>
>
> Java code (simplified):
> Document document = getTheDocument();
> //what to write here???
> NodeIterator nl = XPathAPI.selectNodeIterator(document,
> "/document/element[name=$var]/text()");
>
> This kind of code, as is, throws a RuntimeException about an
> unresolved variable "var". Of course it's unresolved, because I don't
> know how to resolve it!
> The part that says "//what to write here???" should set the
> variable "var" to "foo" so that the node iterator would find this
> node:
> This is an element
>
> How can I do this? Thanks for any help!
>

Is this a trick question or are you getting brain cramp, Joona?
According to the javadocs the second argument is supposed to be
a valid XPath string. Wouldn't that imply something like this?

Advertisements

Sudsy <> writes:
> Is this a trick question or are you getting brain cramp, Joona?
> According to the javadocs the second argument is supposed to be
> a valid XPath string. Wouldn't that imply something like this?
>
> NodeIterator ni = XPathAPI.selectNodeIterator(document,
> "/document/element[@name=\"foo\"]/text()");

His expression was a valid XPath string. Seehttp://www.w3.org/TR/xpath, and notice that VariableReference is a
valid ExprToken element.

Not that I know the answer to his question offhand. :-(
--
See comp.lang.java.announce for java-related announcements

Brian Palmer wrote:
> His expression was a valid XPath string. See
> http://www.w3.org/TR/xpath, and notice that VariableReference is a
> valid ExprToken element.

Where do you think I went for the XPath syntax? As far as the variable
references are concerned, the package doesn't appear to offer the kind
of capabilities found in elements like java.sql.PreparedStatement.
> Not that I know the answer to his question offhand. :-(

In article <>,
Sudsy <> wrote:
>:Joona I Palaste wrote:
>:> In our application's code, we need to use XPath expressions to fetch
>:> data from XML documents. I'm using the Apache Xalan XPath implementation
>:> to do this.
>:> The only thing I've not figured out is how to resolve variables. How
>:> can I set an XPath variable to some value from the Java code?
>:>
>:> For example:
>:> XML code:
>:> <document>
>:> <element name="foo">This is an element</element>
>:> <element name="bar">This is also an element</element>
>:> </document>
>:>
>:> Java code (simplified):
>:> Document document = getTheDocument();
>:> //what to write here???
>:> NodeIterator nl = XPathAPI.selectNodeIterator(document,
>:> "/document/element[name=$var]/text()");
>:>
>:> This kind of code, as is, throws a RuntimeException about an
>:> unresolved variable "var". Of course it's unresolved, because I don't
>:> know how to resolve it!
>:> The part that says "//what to write here???" should set the
>:> variable "var" to "foo" so that the node iterator would find this
>:> node:
>:> This is an element
>:>
>:> How can I do this? Thanks for any help!
>:>
>:
>:Is this a trick question or are you getting brain cramp, Joona?
>:According to the javadocs the second argument is supposed to be
>:a valid XPath string. Wouldn't that imply something like this?
>:
>:NodeIterator ni = XPathAPI.selectNodeIterator(document,
>: "/document/element[@name=\"foo\"]/text()");
>:
>:BTW, that code snippet was tested and returns an interator
>:containing a single element. Note the ampersand and quotes.
>:

What ampersand? That's an "at sign" in my experience, FWIW.

But FYI, the quotes there can be apostrophes (or ticks, single quotes,
or whatever else you usually know them as) unless the data itself
contains them -- and then, I've found that you MUST use quotes there.

This should read:
"/document/element[@name=$var]/text()"
I missed the "at sign" from the original.
NO, there are no quotes around $var. That is intentional. It's
supposed to be the variable named var, not the literal string "$var".
>>:> This kind of code, as is, throws a RuntimeException about an
>>:> unresolved variable "var". Of course it's unresolved, because I don't
>>:> know how to resolve it!
>>:> The part that says "//what to write here???" should set the
>>:> variable "var" to "foo" so that the node iterator would find this
>>:> node:
>>:> This is an element
>>:>
>>:> How can I do this? Thanks for any help!
>>:
>>:Is this a trick question or are you getting brain cramp, Joona?
>>:According to the javadocs the second argument is supposed to be
>>:a valid XPath string. Wouldn't that imply something like this?
>>:
>>:NodeIterator ni = XPathAPI.selectNodeIterator(document,
>>: "/document/element[@name=\"foo\"]/text()");

AFAIK, the string I wrote above *IS* a valid XPath string. The
document at the W3C's home page says variable references are part of
the XPath syntax.
>>:BTW, that code snippet was tested and returns an interator
>>:containing a single element. Note the ampersand and quotes.

I already know how to search for literal strings, thanks very much.
I wouldn't have asked "how can I resolve variables?" if all I ever
had were literal strings.
> What ampersand? That's an "at sign" in my experience, FWIW.
> But FYI, the quotes there can be apostrophes (or ticks, single quotes,
> or whatever else you usually know them as) unless the data itself
> contains them -- and then, I've found that you MUST use quotes there.

Sudsy <> writes:
> Brian Palmer wrote:
> > His expression was a valid XPath string. See
> > http://www.w3.org/TR/xpath, and notice that VariableReference is a
> > valid ExprToken element.
>
> Where do you think I went for the XPath syntax? As far as the variable
> references are concerned, the package doesn't appear to offer the kind
> of capabilities found in elements like java.sql.PreparedStatement.

Xalan does seem to offer it; if you look at XPathContext, you'll see
that a VariableStack can be accessed and set. I just don't know how
the user can make use of that.

I'd suggest Joona check on the xalan-j mailing list.
> > Not that I know the answer to his question offhand. :-(
>
> I just read the docs and ran a quick test. Successfully, I might add.

Joona I Palaste wrote:
> Karen Johns <> scribbled the following:
>>Not sure if this is what you mean (and haven't tried it) but could you not
>>do something along the lines of:
>
>
>>Document document = getTheDocument();
>>String var = //something that sets this to foo
>>String query = "/document/element[name=" + var + "]/text()";
>>NodeIteratro nl = XPathAPI.selectNodeIterator(document, query);
>
>
> No, this is not what I meant at all. "var" is supposed to be an XML
> variable, not a Java variable.

Can you give an example of why you want to be able to use XPath
variables instead of just substiting in literals from Java?

The reason XPath has support for variables is because it was originally
designed to be embedded inside XSLT documents. In order for XPath
expressions to be able to interact with the XSLT document, XSLT has the
ability to define variables, which can be accessed from XPath
expressions. So in XSLT you can define a variable:

There isn't really any need for the XPath variables when using XPath
from Java since Java already has variables of its own. I suppose it
could be nice to have something like PreparedStatement in the java.sql
API where you don't always have to explicitly concatenate strings. In
that case you could use a java.text.MessageFormat object, as in:

"Adam Jenkins" <> wrote in message
news:...
> Joona I Palaste wrote:
> > Karen Johns <> scribbled the following:
> >>Not sure if this is what you mean (and haven't tried it) but could you
not
> >>do something along the lines of:
> >
> >
> >>Document document = getTheDocument();
> >>String var = //something that sets this to foo
> >>String query = "/document/element[name=" + var + "]/text()";
> >>NodeIteratro nl = XPathAPI.selectNodeIterator(document, query);
> >
> >
> > No, this is not what I meant at all. "var" is supposed to be an XML
> > variable, not a Java variable.
>
> Can you give an example of why you want to be able to use XPath
> variables instead of just substiting in literals from Java?

One example would be when the variable contains a node-set rather than a
scalar.

This can't AFAIK be done using XPathAPI. You need to go lower down, to
XPath and XPathContext, though you can use the code in XPathAPI as a guide
for how to call them.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!