Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Xalan XPathAPI question

Reply
Thread Tools

Xalan XPathAPI question

 
 
Joona I Palaste
Guest
Posts: n/a
 
      01-07-2004
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!

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"It was, er, quite bookish."
- Horace Boothroyd
 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      01-07-2004
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.

 
Reply With Quote
 
 
 
 
Brian Palmer
Guest
Posts: n/a
 
      01-08-2004
Sudsy <(E-Mail Removed)> 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. 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.
--
See comp.lang.java.announce for java-related announcements
 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      01-08-2004
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.


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

 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      01-08-2004
"Sudsy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
| 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.




 
Reply With Quote
 
Steve W. Jackson
Guest
Posts: n/a
 
      01-08-2004
In article <(E-Mail Removed)>,
Sudsy <(E-Mail Removed)> 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.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      01-08-2004
Steve W. Jackson <(E-Mail Removed)> scribbled the following:
> In article <(E-Mail Removed)>,
> Sudsy <(E-Mail Removed)> 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 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.


This is irrelevant to my question.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I am not very happy acting pleased whenever prominent scientists overmagnify
intellectual enlightenment."
- Anon
 
Reply With Quote
 
Brian Palmer
Guest
Posts: n/a
 
      01-09-2004
Sudsy <(E-Mail Removed)> 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.


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

--
See comp.lang.java.announce for java-related announcements
 
Reply With Quote
 
Karen Johns
Guest
Posts: n/a
 
      01-09-2004

----- Original Message -----
From: "Joona I Palaste" <(E-Mail Removed)>
Newsgroups: comp.lang.java.programmer
Sent: Thursday, January 08, 2004 5:03 PM
Subject: Re: Xalan XPathAPI question


> Steve W. Jackson <(E-Mail Removed)> scribbled the following:
> > In article <(E-Mail Removed)>,
> > Sudsy <(E-Mail Removed)> 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 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);


> /-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
> \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
> "I am not very happy acting pleased whenever prominent scientists

overmagnify
> intellectual enlightenment."
> - Anon



 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      01-09-2004
Karen Johns <(E-Mail Removed)> scribbled the following:
>> Steve W. Jackson <(E-Mail Removed)> scribbled the following:
>> > In article <(E-Mail Removed)>,
>> > Sudsy <(E-Mail Removed)> 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 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.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Last year he disrespected me - and then he showed lack of respect."
- Anthony Mason
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using XPathAPI with xalan extension functions gkaysen@hotmail.com XML 0 07-07-2005 11:18 AM
Upgrade of Xalan 1.2.2 and Xerces 1.4.4 to Xalan 2.6 and Xerces 2.6.2 cvissy XML 0 11-16-2004 07:06 AM
XPathAPI not obeying XPath properly Richard Bondi Java 2 08-27-2004 12:28 PM
Xalan-j XPathAPI and namespaces Dino Morelli XML 1 11-04-2003 03:24 AM
Re: XPathAPI: a query that returns an int ? Steve W. Jackson Java 2 07-18-2003 09:24 PM



Advertisments