XPath: Select self and following sibling together
If it must be a single XPath 1.0 expression then you'll have to say
//dt[contains(., 'Test')] | //dt[contains(., 'Test')]/following-sibling::dd[1]
The final [1]
is important, as without that it would extract all dd elements that follow a dt containing "Test", i.e. given
<div>
<dt>
Test 1
</dt>
<dd>
Foo
</dd>
<dt>
Something else 2
</dt>
<dd>
Bar
</dd>
</div>
the version without the [1]
would match three nodes, the dt
containing "Test 1" and both the "Foo" and "Bar" dd
elements. With the [1]
you would correctly get only "Test 1" and "Foo".
But depending on exactly how you're using the XPath it may be clearer to first select
//dt[contains(., 'Test')]
and then iterate over the nodes that this matches, and evaluate
. | following-sibling::dd[1]
in the context of each of those nodes in turn.
When using XPath 2.0:
//dt[contains(text(), "Test")]/(self::dt, following-sibling::dd)