Carsten's test case works (with minor adjustments, you need terminate xsl:value-of
with a /) , but always uses <h2>
as the heading. If you want to use different heading elemenents according to the nesting level of the title, then you need something in addition to it:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="title">
<xsl:choose>
<xsl:when test="count(ancestor::section) = 1">
<h1><xsl:value-of select="." /></h1>
</xsl:when>
<xsl:when test="count(ancestor::section) = 2">
<h2><xsl:value-of select="." /></h2>
</xsl:when>
<xsl:otherwise>
<h3><xsl:value-of select="." /></h3>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="para">
<p><xsl:value-of select="." /></p>
</xsl:template>
</xsl:stylesheet>
The XPath function count(ancestor::section)
will return a count of all <section>
elements that are parents of the current element. In the example I have used <h1>
and <h2>
for the two outermost levels and <h3>
for anything deeper nested, but of course you can use other differentiations at your disgression.
It would even be possible to generate the number after the heading on the fly, using this expression:
<xsl:template match="title">
<xsl:variable name="heading">h<xsl:value-of select="count(ancestor::section)" /></xsl:variable>
<xsl:element name="{$heading}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
The xsl:variable
section in there creates a variable with a value of h
+ nesting level. The variable then can be used as a parameter for the xsl:element
element that allows you to dynamically define the name of the element you want to create.
Followup: If you want to use only the h1-h6 as suggested, you could do it like this:
<xsl:template match="title">
<xsl:variable name="hierarchy" select="count(ancestor::section)"/>
<xsl:variable name="heading">h<xsl:value-of select="$hierarchy" /></xsl:variable>
<xsl:choose>
<xsl:when test="$hierarchy > 6">
<h6 class="{$heading}"><xsl:value-of select="." /></h6>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{$heading}">
<xsl:value-of select="." />
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
This expression uses <h6 class="h...">
for anything that has a nesting deeper than 6. It uses <h1>
through <h6>
for all other hierarchy levels.