I don't see why you can't use this:
//table[@id='foo']/tr|//table[@id='foo']/tbody/tr
If you want one expression without node set union:
//tr[(.|parent::tbody)[1]/parent::table[@id='foo']]
In XPath 2.0, the optional step can be expressed as (tbody|.)
.
//table[@id="foo"]/(tbody|.)/tr
XPathTester.com demo
The pipe (|
) denotes union (of two node-sets), the dot (.
) denotes identity step (returning just what the previous step did).
This can be expanded to include more optional elements at once:
//table[@id="foo"]/(thead|tbody|tfoot|.)/tr
Use:
//table[@id="foo"]/*[self::tbody or self::thead or self::tfoot]/tr
|
//table[@id="foo"]/tr
Select any tr
element that is a child of any table
that has an id
attribute "foo" or any tr
element that is a child of a tbody
that is a child any table
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With