html - How to change headers level by XSLT based on depth in XML -
my xsl-file:
... <div> <xsl:choose> <xsl:when test="count(ancestor::node()) = 1"> <h2> </xsl:when> <xsl:when test="count(ancestor::node()) = 2"> <h3> </xsl:when> </xsl:choose> <xsl:attribute name="id"> <xsl:value-of select="@id" /> </xsl:attribute> <xsl:copy-of select="title/node()"/> <xsl:choose> <xsl:when test="count(ancestor::node()) = 1"> </h2> </xsl:when> <xsl:when test="count(ancestor::node()) = 2"> </h3> </xsl:when> </xsl:choose> </div>
i know not allowed split tags h2.../h2, h3.../h3 this.
but how correctly?
you recursive template , generate heading element dynamically.
for example, input xml:
<input> <level id="1"> <title>first</title> <level id="2"> <title>second</title> <level id="3"> <title>third</title> </level> </level> </level> </input>
processed xslt:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" exclude-result-prefixes="xsl"> <xsl:output omit-xml-declaration="yes" indent="yes" method="html"/> <xsl:strip-space elements="*"/> <xsl:template match="level"> <xsl:variable name="level" select="count(ancestor-or-self::level) + 1"/> <xsl:element name="h{$level}"> <xsl:attribute name="id"> <xsl:value-of select="@id"/> </xsl:attribute> <xsl:copy-of select="title/node()"/> </xsl:element> <xsl:apply-templates select="level"/> </xsl:template> </xsl:stylesheet>
gives following html:
<h2 id="1">first</h2> <h3 id="2">second</h3> <h4 id="3">third</h4>
Comments
Post a Comment