I'd start by elevating the value of that stylename attribute to a real element type name, to get access to this information more easily (since it's crucial.) So instead of ><p stylename="heading1" > > <string>More Money for All Amendment > </string> ></p> ><p stylename="section"> > <string>1.(1)Clause 8 (1) (a) of the</string> > <string italic="on">More Money for All Amendment</string> > <string> is deleted and the following substituted:</string> ></p> You have ><heading1> > <string>More Money for All Amendment > </string> ></heading1> ><section> > <string>1.(1)Clause 8 (1) (a) of the</string> > <string italic="on">More Money for All Amendment</string> > <string> is deleted and the following substituted:</string> ></section> You can do this with <xsl:element name="@stylename">...</xsl:element> elements. In general, I think you'll gain quite a bit from pre-processing your RTF-XML into something cleaner and more legible.
> 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]
Since I tackled a problem somewhat like yours back when in a project I distributed, I thought I'd post it.
Note that this recursive string-finding bit doesn't do a case-insensitive match, or make allowances for whitespace (which, judging from a cursory glance at your code, you apparently want). But it might give you a place to start.
Import this stylesheet into yours: