XPath abbreviated form

Discussion in 'XML' started by nils.grimsmo@gmail.com, Jun 25, 2007.

  1. Guest

    What is the the syntax of the abbreviated form of the following XPath
    query?

    child::a/descendant::b[descendant::text()="foo"]

    I try with

    a//b[//text()="foo"]

    but I get two nodes selected instead of one in

    <?xml version="1.0"?>
    <a>
    <x>
    <b>
    <u>foo</u>
    <v>bar</v>
    </b>
    </x>
    <y>
    <b>
    <w>bar</w>
    </b>
    </y>
    </a>

    I am using Jaxen 1.1.1.


    Klem fra Nils
     
    , Jun 25, 2007
    #1
    1. Advertising

  2. Guest

    On Jun 25, 7:29 am, wrote:
    > What is the the syntax of the abbreviated form of the following XPath
    > query?
    >
    > child::a/descendant::b[descendant::text()="foo"]
    >
    > I try with
    >
    > a//b[//text()="foo"]
    >
    > but I get two nodes selected instead of one in
    >
    > <?xml version="1.0"?>
    > <a>
    > <x>
    > <b>
    > <u>foo</u>
    > <v>bar</v>
    > </b>
    > </x>
    > <y>
    > <b>
    > <w>bar</w>
    > </b>
    > </y>
    > </a>
    >
    > I am using Jaxen 1.1.1.
    >
    > Klem fra Nils


    The expression below should work and return only one node

    a//b[.//text() = "foo"]

    This provides only one node by prefixing '//' with '.' (self) in the
    predicate.



    Phil Fearon
    http://www.sketchpath.com
     
    , Jun 25, 2007
    #2
    1. Advertising

  3. In article <>,
    <> wrote:
    >What is the the syntax of the abbreviated form of the following XPath
    >query?
    >
    > child::a/descendant::b[descendant::text()="foo"]
    >
    >I try with
    >
    > a//b[//text()="foo"]


    the "//text()" in the predicate means and text descendants of the root
    node. To find descendants of the context node, you need ".//text()".

    Incidentally, "//" isn't exactly the same as "descendant". In
    particular, "//*[1]" is not the same as "/descendant::*[1]", but it
    doesn't make any difference in this case.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Jun 25, 2007
    #3
  4. a écrit :
    > What is the the syntax of the abbreviated form of the following XPath
    > query?
    >
    > child::a/descendant::b[descendant::text()="foo"]
    >
    > I try with
    >
    > a//b[//text()="foo"]
    >
    > but I get two nodes selected instead of one in
    >


    // is short for /descendant-or-self::node()/

    a//b[//text()="foo"]
    means
    a//b[/descendant-or-self::node()/="foo"]
    it starts from the root

    a//b[.//text()="foo"]
    means
    a//b[./descendant-or-self::node()/="foo"]
    it doesn't start from the root
     
    Philippe Poulard, Jun 25, 2007
    #4
  5. Me thinks this:

    a//b[.//text()='foo']

    What appears to be wrong with
    a//b[//text()="foo"]
    is that you are selecting descendants of the entire document root in the
    predicate //text() whereas you really only want descendants of b
    elements. Therefore you need to put a dot before the slashslash in the
    predicate.

    Cheers
    Justin Johansson




    wrote:
    > What is the the syntax of the abbreviated form of the following XPath
    > query?
    >
    > child::a/descendant::b[descendant::text()="foo"]
    >
    > I try with
    >
    > a//b[//text()="foo"]
    >
    > but I get two nodes selected instead of one in
    >
    > <?xml version="1.0"?>
    > <a>
    > <x>
    > <b>
    > <u>foo</u>
    > <v>bar</v>
    > </b>
    > </x>
    > <y>
    > <b>
    > <w>bar</w>
    > </b>
    > </y>
    > </a>
    >
    > I am using Jaxen 1.1.1.
    >
    >
    > Klem fra Nils
    >
     
    Justin Johansson, Jun 25, 2007
    #5
    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. Mike H
    Replies:
    1
    Views:
    706
    Chris Smith
    Feb 27, 2004
  2. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    1,976
    jan V
    Jul 29, 2005
  3. Iñaki Baz Castillo

    Is faster using abbreviated parameter names?

    Iñaki Baz Castillo, Mar 2, 2009, in forum: Ruby
    Replies:
    7
    Views:
    119
    Denis Haskin
    Mar 3, 2009
  4. Jörg W Mittag
    Replies:
    1
    Views:
    97
    Rick DeNatale
    Jun 7, 2010
  5. Roedy Green

    abbreviated generic syntax

    Roedy Green, Feb 18, 2013, in forum: Java
    Replies:
    14
    Views:
    292
Loading...

Share This Page