Project

Profile

Help

counting specific elements » tab-list.xsl

XSLT 3 - Mark Hutchinson, 2025-01-13 22:07

 
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
exclude-result-prefixes="xs math"
version="3.0">

<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="entry">
<xsl:element name="entry">
<xsl:copy-of select="@* except @written"/>
<xsl:apply-templates select="node()" mode="count"/>
</xsl:element>
</xsl:template>

<xsl:template match="*" mode="count">
<xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="text()" mode="count">
<xsl:copy-of select="."/>
</xsl:template>

<!-- Mode to count and set the c attribute -->
<xsl:template match="p | ul | ol" mode="count">
<xsl:variable name="count" select="count(preceding::p[ancestor::entry[1]] | preceding::ul[ancestor::entry[1]] | preceding::ol[ancestor::entry[1]]) + 1"/>
<xsl:element name="{name()}">
<xsl:attribute name="c" select="$count"/>
<xsl:copy-of select="@* except @written"/>
<xsl:apply-templates select="node()" mode="count"/>
</xsl:element>
</xsl:template>

</xsl:stylesheet>
(1-1/3)