FordPrefect141
FordPrefect141

Reputation: 205

How to get text from a preceding node using xpath

I am an experienced developer but I am something of a newbie to xpath and xslt which my company has suddenly decided to implement and I am hoping someone might be able to help me with a challenge I have.

If I have XML like this.....

<tr layoutcode="" type="categoryhead" level="2"
   <td colname="caption">Alex</td>     (a)
</tr>
<tr layoutcode="" type="categoryhead" level="3"
   <td colname="caption">Miscellaneous</td>
</tr>
<tr layoutcode="" type="detail" level="4"
   <td colname="caption">Something</td>
</tr>
<tr layoutcode="" type="detail" level="4"
   <td colname="caption">This is a test</td>   (b)
</tr>

... and I am on the 'This is a test' node (b), how do I get the get the text from (a) i.e. 'Alex'?

All I know is that I am looking for the first preceding 'tr' node with attributes of 'type' = 'categoryhead' and 'level' = 2, then I want the text from its 'td' child node. I guess I am looking for the right xpath query so I can assign it to a variable.

Many thanks in advance, Alex

Upvotes: 0

Views: 135

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167446

The path ../preceding-sibling::tr[@type = 'categoryhead' and @level = 2][1]/td[@colname = 'caption'] should select the td element.

Upvotes: 2

Related Questions