tags:

views:

774

answers:

1

Is the following possible in one XPath expression (see sample below):

Select all (span tags of class msg) AND all ((img tags that have a non-empty alt attribute) AND (are NOT located inside a span tag of class msg))

Simplified sample:

<span class="msg">Message text A</span>
<img alt="" />
<span class="msg">Message text B <img alt="text A" /></span>
<span class="err">Error text C <img alt="text B" /></span>
<img alt="text C" />
<span class="err">Error text D</span>

The resulting node set should contain:

<span class="msg">Message text A</span>
<span class="msg">Message text B <img alt="text A" /></span>
<img alt="text B" />    
<img alt="text C" />
+6  A: 

Not tested, but something like?

//span[@class='msg'] | //img[@alt!='' and not(ancestor::span[@class='msg'])]
Marc Gravell
Thanks, this works perfect! I removed the first "not(ancestor::..." since the material can not contain msg span inside msg spans.It saved two iterations through the same data, thus speeding up things.
barry
OK - I miscounted some brackets ;-p
Marc Gravell