Xalan XPathAPI question

  • Thread starter Joona I Palaste
  • Start date
J

Joona I Palaste

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!
 
S

Sudsy

Joona said:
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.
 
B

Brian Palmer

Sudsy said:
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. See
http://www.w3.org/TR/xpath, and notice that VariableReference is a
valid ExprToken element.

Not that I know the answer to his question offhand. :-(
 
S

Sudsy

Brian said:
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. :-(

I just read the docs and ran a quick test. Successfully, I might add.
 
A

Andrew Thompson

| Brian Palmer wrote:
....
| > Not that I know the answer to his question offhand. :-(
|
| I just read the docs and ran a quick test. Successfully, I
might add.

:)
 
S

Steve W. Jackson

Sudsy said:
: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.

= Steve =
 
J

Joona I Palaste

Steve W. Jackson said:
: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 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.

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.

This is irrelevant to my question.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I am not very happy acting pleased whenever prominent scientists overmagnify
intellectual enlightenment."
- Anon
 
B

Brian Palmer

Sudsy said:
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.
I just read the docs and ran a quick test. Successfully, I might add.

I don't think your solution is fully applicable to what was asked.
 
K

Karen Johns

----- Original Message -----
From: "Joona I Palaste" <[email protected]>
Newsgroups: comp.lang.java.programmer
Sent: Thursday, January 08, 2004 5:03 PM
Subject: Re: Xalan XPathAPI question

Steve W. Jackson said:
: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 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".


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);
 
J

Joona I Palaste

Karen Johns said:
Steve W. Jackson said:
:> 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 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".
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.
 
A

Adam Jenkins

Joona said:
Karen Johns said:
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:

<xsl:variable name="var">value</xsl:variable>

And later use the variable in an XPath expression:

<xsl:value-of select="/document/element[name=$var]"/>

When using XPath from Java instead from XSLT, the equivalent would be

String var = "value";

XPathAPI.selectSingleNode(doc,
"/document/element[name=' " + var + "']");

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:

Format xpathFormat = new MessageFormat("/document/element[name='{0}']");

String var = "value";
String xpath = xpathFormat.format(new Object[]{var});

Use a different value:

var = "another value";
xpath = xpathFormat.format(new Object[]{var});
 
M

Mike Schilling

Adam Jenkins said:
Joona said:
Karen Johns said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top