Xalan XPathAPI question

Discussion in 'Java' started by Joona I Palaste, Jan 7, 2004.

  1. 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "It was, er, quite bookish."
    - Horace Boothroyd
     
    Joona I Palaste, Jan 7, 2004
    #1
    1. Advertising

  2. Joona I Palaste

    Sudsy Guest

    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.
     
    Sudsy, Jan 7, 2004
    #2
    1. Advertising

  3. Joona I Palaste

    Brian Palmer Guest

    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. 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
     
    Brian Palmer, Jan 8, 2004
    #3
  4. Joona I Palaste

    Sudsy Guest

    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.
     
    Sudsy, Jan 8, 2004
    #4
  5. "Sudsy" <> wrote in message
    news:...
    | 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.

    :)
     
    Andrew Thompson, Jan 8, 2004
    #5
  6. 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.

    = Steve =
    --
    Steve W. Jackson
    Montgomery, Alabama
     
    Steve W. Jackson, Jan 8, 2004
    #6
  7. Steve W. Jackson <> scribbled the following:
    > 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 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "I am not very happy acting pleased whenever prominent scientists overmagnify
    intellectual enlightenment."
    - Anon
     
    Joona I Palaste, Jan 8, 2004
    #7
  8. Joona I Palaste

    Brian Palmer Guest

    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.


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

    --
    See comp.lang.java.announce for java-related announcements
     
    Brian Palmer, Jan 9, 2004
    #8
  9. Joona I Palaste

    Karen Johns Guest

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


    > Steve W. Jackson <> scribbled the following:
    > > 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 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 () ------------- Finland --------\
    > \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    > "I am not very happy acting pleased whenever prominent scientists

    overmagnify
    > intellectual enlightenment."
    > - Anon
     
    Karen Johns, Jan 9, 2004
    #9
  10. Karen Johns <> scribbled the following:
    >> Steve W. Jackson <> scribbled the following:
    >> > 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 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Last year he disrespected me - and then he showed lack of respect."
    - Anthony Mason
     
    Joona I Palaste, Jan 9, 2004
    #10
  11. Joona I Palaste

    Adam Jenkins Guest

    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:

    <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});
     
    Adam Jenkins, Jan 9, 2004
    #11
  12. "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.
     
    Mike Schilling, Jan 11, 2004
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve W. Jackson

    Re: XPathAPI: a query that returns an int ?

    Steve W. Jackson, Jul 17, 2003, in forum: Java
    Replies:
    2
    Views:
    477
    Steve W. Jackson
    Jul 18, 2003
  2. Richard Bondi

    XPathAPI not obeying XPath properly

    Richard Bondi, Aug 25, 2004, in forum: Java
    Replies:
    2
    Views:
    679
    Richard Bondi
    Aug 27, 2004
  3. Dino Morelli

    Xalan-j XPathAPI and namespaces

    Dino Morelli, Nov 3, 2003, in forum: XML
    Replies:
    1
    Views:
    3,259
    Dino Morelli
    Nov 4, 2003
  4. cvissy
    Replies:
    0
    Views:
    630
    cvissy
    Nov 16, 2004
  5. Replies:
    0
    Views:
    452
Loading...

Share This Page