Faced with this:
<div>
some text
<!-- this is the hook comment-->
target part 1
target part 2
<!-- this is another comment-->
some other text
</div>
I'm trying to get to the desired output of:
target part 1 target part 2
The number of comments and text elements is unknown, but the target text always comes after the comment containing hook
. So the idea is to find the position()
of the relevant comment()
, and get the next element.
There are some previous questions about finding the position of an element containing a certain text or by attribute, but comment()
is an odd duck and I can't modify the answers there to this situation. For example, trying a variation on the answers:
//comment()[contains(string(),'hook')]/preceding::*
or using preceding-sibling::*
, returns nothing.
So I decided to try something else. A count(//node())
of the xml returns 6
. And //node()[2]
returns the relevant comment()
. But when I try to get the position of that comment by using index-of()
(which should return 2
)
index-of(//node(),//comment()[contains(string(),'hook')])
it returns 3
!
Of course, I can disregard that and use the 3
index position as the position for the target text (instead of incrementing 2
by 1), but I was wondering, first, why is the outcome what it is and, second, does it have any unintended consequences.