I can choose several paths using .//div/h1/text() | .//div/h2/text(). However I would like to know if there’s a way of doing it without explicitly writing out the part that is common for both path’s – in this case .//div/ – every time?
I can choose several paths using .//div/h1/text() | .//div/h2/text() . However I would like
Share
As for shortcuts, with XPath 2.0 you can shorten e.g.
//div/h1 | //div/h2to e.g.//div/(h1 | h2)but that syntax is not allowed in XPath 1.0. And I think XPath 3.0 will introduce aletclause to define variables. So there I think you can do e.g.let $r := /html/body/div[3]/table[2]/tbody/tr[5] return ($r/span | $r/a).Or for your corrected sample with XPath 2.0 you can shorten
.//div/h1/text() | .//div/h2/text()to.//div/(h1/text() | h2/text()). But with XPath 1.0 all you can do is use.//div/*[self::h1 | self::h2]/text().