Skip to content

Instantly share code, notes, and snippets.

@ScalaWilliam
Last active December 28, 2015 17:29
Show Gist options
  • Save ScalaWilliam/7536673 to your computer and use it in GitHub Desktop.
Save ScalaWilliam/7536673 to your computer and use it in GitHub Desktop.
Introducing recursion and then flattening the result with Saxon. Desire: do section numbering and grouping without using counters. Platform: Saxon 6.5.5
<?xml version="1.0" encoding="UTF-8"?>
<root>
<heading>Hello</heading>
<paragraph>This is my first paragraph</paragraph>
<paragraph>This is my second paragraph</paragraph>
<heading>Hello!</heading>
<heading>HelloOooo!</heading>
<paragraph>Coolness.</paragraph>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:saxon="http://icl.com/saxon"
exclude-result-prefixes="saxon"
version="1.0">
<xsl:output indent="yes"/>
<xsl:template name="numbering">
<xsl:param name="position"/>
<xsl:param name="node"/>
<xsl:variable name="next" select="saxon:node-set(($node/following::heading)[1])"/>
<section>
<h1><xsl:value-of select="$position"/>. <xsl:value-of select="."/></h1>
<xsl:for-each select="saxon:intersection($node/following::paragraph, $next/preceding::paragraph)">
<p><xsl:value-of select="."/></p>
</xsl:for-each>
</section>
<xsl:if test="$next">
<xsl:call-template name="numbering">
<xsl:with-param name="position" select="$position + 1"/>
<xsl:with-param name="node" select="$next"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="/root/heading[1]">
<document>
<xsl:call-template name="numbering">
<xsl:with-param name="position" select="1"/>
<xsl:with-param name="node" select="saxon:node-set(.)"/>
</xsl:call-template>
</document>
</xsl:template>
<xsl:template match="text()"></xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="utf-8"?>
<document>
<section>
<h1>1. Hello</h1>
<p>This is my first paragraph</p>
<p>This is my second paragraph</p>
</section>
<section>
<h1>2. Hello</h1>
</section>
<section>
<h1>3. Hello</h1>
</section>
</document
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment