Thursday, 22 August 2013

Xpath: How to find an element meeting certain criteria that comes before another element

Xpath: How to find an element meeting certain criteria that comes before
another element

Task:
With the context node being any of the <pic> elements (my processing is
within a match="pic" template), how do I return the first <a> element that
meets both of the following criteria?
Comes before the next <pic> element, AND
Contains the string 'blah'
Assumptions:
<pic> elements are sometimes siblings of other <pic> elements, but not
always. Nor are they always siblings of other <a> elements.
<a> elements are sometimes siblings of other <a> elements, but not always.
Here's the XML structure:
<document>
<pic>
<img>
</pic>
....
<ul>
<li>
<pic>
<img>
</pic>
</li>
<li>
<a href="someFile.html"> <!-- doesn't meet both criteria -->
</li>
</ul>
<p>
<a href="anotherFile.html"> <!-- doesn't meet both criteria -->
<pic>
<img>
</pic>
...
<a href="...blah....html"> <!-- meets both criteria -->
<pic>
<img>
</pic>
...
<p>
<a href="someOtherFile.html> <!-- doesn't meet both criteria -->
<p>
...
<pic>
<img>
</pic>
...
<a href="...blah....html"> <!-- meets both criteria -->
...
<pic>
<img>
</pic>
...

No comments:

Post a Comment