Re: [xsl] Need for a technique

Subject: Re: [xsl] Need for a technique
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 13 Feb 2001 13:05:13 +0000
Hi Paul,

> In this example, 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


  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]


  ancestor::*[starts-with(name(), 'h') and
              number(substring(name(), 2))][1]

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread