tags:

views:

74

answers:

1

Hi,

I got html code like the following:

<p style="margin:0 0 0.5em 0;"><b>Blablub</b></p>
<table> ... </table>

Now I want to query the content of the <b> right above the table but only if the table does not have any attributes. I tried the following query:

//table[not(@*)]/preceding-sibling::p/b

If I remove the preceding-sibling::p/b part entirely it works. It gives me exactly the tables I need. However, if I use this query it gives me content of an <b> tag which preceds a table WITH attributes.

I'm clueless. Anyone got an idea?

+1  A: 

Use:

//table[not(@*)]/preceding-sibling::*[1][self::p]/b

This means: Select all b elements that are children of all p elements that are the first preceding sibling of a table that has no attributes.

This is quite different from the problematic expression cited in the question:

//table[not(@*)]/preceding-sibling::p[1]/b

The latter selects the b children of the first p following sibling -- there is no guarantee that the first p following sibling is also the first element sibling.

Dimitre Novatchev
Thanks a lot. This seems to work, alhtough I don't really understand why it didn't work before :). I'm using the query '//table[not(@*)]/preceding-sibling::p[1]/b' which should be the same as yours.
Christian Ziegler
@Christian-Ziegler: These two expressions are not equivalent. Yours selects the `b` children of the first `p` preceding sibling. Mine selects the `b` children of the first element sibling, if it happens to be a `p`.
Dimitre Novatchev
Ahh that makes sense. Thanks for clarification!
Christian Ziegler