XSLT and sectN/section
This came up during a recent Pandoc
discussion.
The discussion was about outputting <sectN> section styling when creating DocBook XML. Currently
Pandoc outputs nested <section>s.
I argued you could easily change between the two formats and <section> is more flexible, so
just leave Pandoc as it is. But it allowed me to play with XSLT once more. With the following
results.
Translate to sectN⌗
This XSLT translates <section> to <sectN> where N is 5. If the sections are nested deeper
it switches to <section>.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="section">
<xsl:variable name="i" select="count(ancestor::node())"/>
<xsl:choose>
<xsl:when test="$i > 5">
<section><xsl:apply-templates/></section>
</xsl:when>
<xsl:otherwise>
<xsl:element name="sect{$i}"><xsl:apply-templates/></xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Translate to section
And this translate <sectN> to <section>, this was slightly easier, as you
don’t have to count anything.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="sect1 | sect2 | sect3 | sect4 | sect5">
<section><xsl:apply-templates/></section>
</xsl:template>
</xsl:stylesheet>
Read other posts