I have a XML document with chapters
and nested sections
.
I am trying to find, for any section, the first second-level section ancestor.
That is the next-to-last section in the ancestor-or-self
axis.
pseudo-code:
<chapter><title>mychapter</title>
<section><title>first</title>
<section><title>second</title>
<more/><stuff/>
</section>
</section>
</chapter>
my selector:
<xsl:apply-templates
select="ancestor-or-self::section[last()-1]" mode="title.markup" />
Of course that works until last()-1 isn't defined (the current node is the first
section).
If the current node is below the second
section, i want the title second
.
Otherwise I want the title first
.
Replace your xpath with this:
ancestor-or-self::section[position()=last()-1 or count(ancestor::section)=0][1]
Since you can already find the right node in all cases except one, I updated your xpath to also find the first
section (or count(ancestor::section)=0
), and then select ([1]
) the first match (in reverse document order, since we are using the ancestor-or-self
axis).