how to select parent?

T

Timo Nentwig

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
 
D

DFN-CIS NetNews Service

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']]
 
S

Soren Kuula

Timo said:
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
 
C

C. M. Sperberg-McQueen

Timo Nentwig said:
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
 
P

Patrick TJ McPhee

% 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"]/..
 
D

Dimitre Novatchev

Timo Nentwig said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top