0
votes

Given the following XML, how do I select the uid element whose preceding-siblings are comments with normalized text value equal to "VID_1128" or "Some Title"? The problem here is that "Some Title" precedes two <uid> elements, so I only want to select the uid element whose value is 3333. Is this possible to do with XPath 1.0, and if so how?

<?xml version="1.0"?>
<uids>
    <!-- Some Unique Text -->
    <!-- VID_2564 -->
    <uid>1111</uid>
    <!-- Some Title -->
    <!-- VID_8374 -->
    <uid>2222</uid>
    <!-- Some Title -->
    <!-- VID_1128 -->
    <uid>3333</uid>
    <!-- A Different Title -->
    <!-- VID_9581 -->
    <uid>4444</uid>
</uids>
1

1 Answers

0
votes

This is possible with XPath-1.0 and the way to do it is the following:

/uids/uid[normalize-space(preceding-sibling::comment()[1]) = 'VID_1128' and normalize-space(preceding-sibling::comment()[2]) = 'Some Title']

There is one difference to your given objective:

are comments with normalized text value equal to "VID_1128" or "Some Title"?

The above XPath expression follows the condition of and and not or between the two predicates. This has to be done to select only 3333 as result.

Using or instead of and would result in a nodeset with two values: 2222 and 3333 - and that's not what you wanted.