Using
/*/daughter[@state = 'nice'][1] | /*/daughter[@state = 'nice'][1] /following-sibling::daughter[@state='naughty'] [1]
Here a couple of the first pleasant daughter and her closest naughty daughter are selected.
To select the second such pair, use:
/*/daughter[@state = 'nice'][2] | /*/daughter[@state = 'nice'][2] /following-sibling::daughter[@state='naughty'] [1]
... etc.
Note that these expressions do not guarantee that the node parameter will be selected at all - there cannot be daughter elements, or not every nice daughter element can have the next sibling daughter element, which is naughty.
If it is guaranteed that the document has strictly daughter elements strictly ( 'nice' , 'naughty ), then you can use a very simple XPath expression to get all pairs
/ * / daughter [@state = 'nice' or @state = 'naughty']
This selects all daughter elements that are children of the top element, and have a variable state attribute with values: nice, naughty, nice, naughty, ...
If the XPath API used gets them in an array of objects , then for each even k pair of daughters is in the kth and (k + 1) -th members of this array.
source share