xpath descendant question

Discussion in 'XML' started by CAFxX, Feb 4, 2009.

  1. CAFxX

    CAFxX Guest

    Hi everybody, I have a problem with a xpath query.
    Suppose you have something like this:

    <a>
    <b>X</b>
    <a>
    <c>
    <b>Y</b>
    </c>
    </a>
    </a>

    Right now to obtain a list of <a> containg a certain token I do:

    //a[ contains( descendant-or-self::*, 'token' ) ]

    the problem is that, in the case above, searching for the token Y would
    give both <a>s, whereas I need only the innermost one (i.e. the nearest
    <a> ancestor of the fragment containing the searched token). How can I
    translate this in xpath?
    Thank you and best regards,
    CAFxX
    CAFxX, Feb 4, 2009
    #1
    1. Advertising

  2. CAFxX

    Peter Flynn Guest

    CAFxX wrote:
    > Hi everybody, I have a problem with a xpath query.
    > Suppose you have something like this:
    >
    > <a>
    > <b>X</b>
    > <a>
    > <c>
    > <b>Y</b>
    > </c>
    > </a>
    > </a>
    >
    > Right now to obtain a list of <a> containg a certain token I do:
    >
    > //a[ contains( descendant-or-self::*, 'token' ) ]
    >
    > the problem is that, in the case above, searching for the token Y would
    > give both <a>s, whereas I need only the innermost one (i.e. the nearest
    > <a> ancestor of the fragment containing the searched token). How can I
    > translate this in xpath?


    //*[contains(.,'Y')]/ancestor::a[1]

    ///Peter
    Peter Flynn, Feb 4, 2009
    #2
    1. Advertising

  3. CAFxX

    CAFxX Guest

    Ok, I suddenly realized what axes are all about. Thank you very much!

    Peter Flynn ha scritto:
    > CAFxX wrote:
    >> Hi everybody, I have a problem with a xpath query.
    >> Suppose you have something like this:
    >>
    >> <a>
    >> <b>X</b>
    >> <a>
    >> <c>
    >> <b>Y</b>
    >> </c>
    >> </a>
    >> </a>
    >>
    >> Right now to obtain a list of <a> containg a certain token I do:
    >>
    >> //a[ contains( descendant-or-self::*, 'token' ) ]
    >>
    >> the problem is that, in the case above, searching for the token Y
    >> would give both <a>s, whereas I need only the innermost one (i.e. the
    >> nearest <a> ancestor of the fragment containing the searched token).
    >> How can I translate this in xpath?

    >
    > //*[contains(.,'Y')]/ancestor::a[1]
    >
    > ///Peter
    CAFxX, Feb 5, 2009
    #3
  4. CAFxX

    CAFxX Guest

    I found just a minor error in your code, it should be

    //*[contains(child::text(),'Y')]/ancestor::a[1]

    and not

    //*[contains(.,'Y')]/ancestor::a[1]

    right?

    Peter Flynn ha scritto:
    > CAFxX wrote:
    >> Hi everybody, I have a problem with a xpath query.
    >> Suppose you have something like this:
    >>
    >> <a>
    >> <b>X</b>
    >> <a>
    >> <c>
    >> <b>Y</b>
    >> </c>
    >> </a>
    >> </a>
    >>
    >> Right now to obtain a list of <a> containg a certain token I do:
    >>
    >> //a[ contains( descendant-or-self::*, 'token' ) ]
    >>
    >> the problem is that, in the case above, searching for the token Y
    >> would give both <a>s, whereas I need only the innermost one (i.e. the
    >> nearest <a> ancestor of the fragment containing the searched token).
    >> How can I translate this in xpath?

    >
    > //*[contains(.,'Y')]/ancestor::a[1]
    >
    > ///Peter
    CAFxX, Feb 5, 2009
    #4
  5. CAFxX

    Peter Flynn Guest

    CAFxX wrote:
    > I found just a minor error in your code, it should be
    >
    > //*[contains(child::text(),'Y')]/ancestor::a[1]
    >
    > and not
    >
    > //*[contains(.,'Y')]/ancestor::a[1]
    >
    > right?


    In some circumstances...it depends on what other markup is in your
    real-life document. If you want to find all a elements which have a Y in
    any element within them, regardless of how deep, then my example is
    correct, because <a><b>Y</b></a> contains Y somewhere in it.But the same
    is then true of your outer a as well, because
    <a><b>X</b><a><c><b>Y</b></c></a></a> contains XY, which contains a Y.

    If what you really want is the a ancestor of all b elements that
    directly contain Y, then you need to specify that:

    //b[contains(text(),'Y')]/ancestor::a[1]

    This will return your nested a element rather than both a elements.

    ///Peter
    --
    XML FAQ: http://xml.silmaril.ie/


    > Peter Flynn ha scritto:
    >> CAFxX wrote:
    >>> Hi everybody, I have a problem with a xpath query.
    >>> Suppose you have something like this:
    >>>
    >>> <a>
    >>> <b>X</b>
    >>> <a>
    >>> <c>
    >>> <b>Y</b>
    >>> </c>
    >>> </a>
    >>> </a>
    >>>
    >>> Right now to obtain a list of <a> containg a certain token I do:
    >>>
    >>> //a[ contains( descendant-or-self::*, 'token' ) ]
    >>>
    >>> the problem is that, in the case above, searching for the token Y
    >>> would give both <a>s, whereas I need only the innermost one (i.e. the
    >>> nearest <a> ancestor of the fragment containing the searched token).
    >>> How can I translate this in xpath?

    >>
    >> //*[contains(.,'Y')]/ancestor::a[1]
    >>
    >> ///Peter
    Peter Flynn, Feb 7, 2009
    #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. Nik Coughin

    css - descendant selectors

    Nik Coughin, Jan 28, 2004, in forum: HTML
    Replies:
    2
    Views:
    387
    Alliss
    Jan 29, 2004
  2. Ales DOLECEK

    Templates and descendant classes

    Ales DOLECEK, Jul 22, 2003, in forum: C++
    Replies:
    2
    Views:
    433
    Rob Williscroft
    Jul 22, 2003
  3. Reid Priedhorsky
    Replies:
    2
    Views:
    331
    Bengt Richter
    Jul 12, 2005
  4. Paul Smitton

    const data in descendant classes

    Paul Smitton, Aug 20, 2007, in forum: C++
    Replies:
    5
    Views:
    340
    Paul Smitton
    Aug 21, 2007
  5. njsimha
    Replies:
    0
    Views:
    779
    njsimha
    Sep 16, 2008
Loading...

Share This Page