Jeni Tennison
> I am looking for the nearest enclosing <hx> element (h1 or h2).
>
> I need an Xpath expression that would give back h1 for the first <x/>
> element, and h2 for the second.
You can get the 'enclosing' elements with the ancestor:: axis. So you
want to look along the ancestor:: axis and find any elements that are
called h1 or h2:
ancestor::h1 | ancestor::h2
or:
ancestor::*[self::h1 or self::h2]
or, if you want any element whose name starts with 'h' and ends in a
number, then:
ancestor::*[starts-with(name(), 'h') and
number(substring(name(), 2))]
and so on.
You want the nearest of these, which you can identify by studying its
position in the document relative to the other nodes in the set. The
first expression (ancestor::h1 | ancestor::h2) is a union of two node
sets, so if you use a positional predicate on that, the positions will
be judged in document order, and you want the last:
(ancestor::h1 | ancestor::h2)[last()]
The other expressions are single steps, and the ancestor:: axis is a
backwards axis, so any positional predicates will be assessed with the
node set in reverse document order, and you need:
ancestor::*[self::h1 or self::h2][1]
or:
ancestor::*[starts-with(name(), 'h') and
number(substring(name(), 2))][1]
|