Consider the following HTML:
<div>
text1
</div>
<div>
<span>
text2
</span>
</div>
<div>
text3
</div>
I need to select all the nodes with text1/text2/text3. When I use
/html/body/div[position() > 0]
I obviously don't get the span
around text2, but the div
around <span>text2</span>
. How can I say: If there is a span
following the div
, then return the span
; if the div
is already the last element in a path, return the div
? So the intended nodes would be:
div[0]
div[1]/span
div[2]
Update: This one works, but is there a shorter way to do it? (e.g. I am writing /html/body/div
in both of them, is it possible to make the pipe symbol (or) at a later place?)
/html/body/div[position() > 0 and count(*) = 0] | /html/body/div[position() > 0]/span