how to select parent?

Discussion in 'XML' started by Timo Nentwig, Feb 2, 2004.

  1. Timo Nentwig

    Timo Nentwig Guest

    Hi!

    I want to select the parent of <span class="h2"> (which is <td>). The
    following does not work:

    //span[@class="h2"]/../td

    <blah>
    <td>
    <span class="h2" />
    </td>
    </blah>

    Why not?

    Regards,
    Timo
     
    Timo Nentwig, Feb 2, 2004
    #1
    1. Advertising

  2. On 02/02/2004, around 13:32, Timo Nentwig wrote:
    TN> Hi!

    TN> I want to select the parent of <span class="h2"> (which is <td>). The
    TN> following does not work:

    TN> //span[@class="h2"]/../td
    Try

    //td[child::span[@class='h2']]

    --
    Stuart
    If love is blind, why is lingerie so popular?
     
    DFN-CIS NetNews Service, Feb 2, 2004
    #2
    1. Advertising

  3. Timo Nentwig

    Soren Kuula Guest

    Timo Nentwig wrote:
    > Hi!
    >
    > I want to select the parent of <span class="h2"> (which is <td>). The
    > following does not work:
    >
    > //span[@class="h2"]/../td


    Is that XSL ?

    I think it's because you are not allowed to select ancestor nodes
    (creating loops) - on the other hand, it's OK to evaluate (value-of) them.

    But as somebody else suggested, the fix is instead to specifially select
    the parents whose children have the desired properties.

    Soren
    --
    Fjern de 4 bogstaver i min mailadresse som er indsat for at hindre s...
    Remove the 4 letter word meaning "junk mail" in my mail address.
     
    Soren Kuula, Feb 2, 2004
    #3
  4. Timo Nentwig <> writes:

    > Hi!
    >
    > I want to select the parent of <span class="h2"> (which is <td>). The
    > following does not work:
    >
    > //span[@class="h2"]/../td


    Because it selects each sibling of the span element which is
    named td.

    If you want the parent whatever it's name, try

    //span[@class="h2"]/..

    If you want the parent if and only if it's named 'td', try

    //span[@class="h2"]/parent::td

    I hope this helps.

    -C. M. Sperberg-McQueen
     
    C. M. Sperberg-McQueen, Feb 2, 2004
    #4
  5. In article <bvlj15$te09t$-berlin.de>,
    Timo Nentwig <> wrote:

    % I want to select the parent of <span class="h2"> (which is <td>). The
    % following does not work:

    % //span[@class="h2"]/../td

    This is looking for siblings of the span called td. This should do what
    you want:

    //span[@class="h2"]/..

    --

    Patrick TJ McPhee
    East York Canada
     
    Patrick TJ McPhee, Feb 2, 2004
    #5
  6. "Timo Nentwig" <> wrote in message
    news:bvlj15$te09t$-berlin.de...
    > Hi!
    >
    > I want to select the parent of <span class="h2"> (which is <td>). The
    > following does not work:
    >
    > //span[@class="h2"]/../td
    >
    > <blah>
    > <td>
    > <span class="h2" />
    > </td>
    > </blah>
    >
    > Why not?


    Michael already explained why -- this expression is selecting all siblings
    "td" of all "span" elements, whose "class" attribute is "h2".

    On the other side the seemingly correct XPath expression:

    //span[@class="h2"]/..

    does not select *the* parent of "span" -- it selects *all* parents of all
    "span" elements, whose "class" attribute is "h2".

    If you want the (one and only) parent of a specific single "span" element,
    you shouldn't be using the "//" abbreviation.


    Cheers,

    Dimitre Novatchev [XML MVP],
    FXSL developer, XML Insider,

    http://fxsl.sourceforge.net/ -- the home of FXSL
    Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html
     
    Dimitre Novatchev, Feb 2, 2004
    #6
    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. Roy
    Replies:
    2
    Views:
    3,173
  2. mark
    Replies:
    1
    Views:
    600
  3. metal
    Replies:
    8
    Views:
    530
    Bruno Desthuilliers
    Oct 30, 2009
  4. palmiere
    Replies:
    1
    Views:
    474
    Erwin Moller
    Feb 9, 2004
  5. Bill S.
    Replies:
    2
    Views:
    241
    Bill S.
    May 8, 2004
Loading...

Share This Page