Expand | Collapse

◀ 18. Support for literate programming20. Site maps ▶

19. Numbering

In general, automatic numbering of sections requires knowledge of the global structure of the document.

There are thus two versions of automatic numbering implemented. One works using the information from a site map. The other one works when the site map is missing — that is nevertheless useful when processing single-file documents. In theory, the two methods can be combined too, if the site map exists but is incomplete for the target document.

The hierarchical numbering process is started by invoking the number template on the target node.

xsl:stylesheet id="stylesheet" exclude-result-prefixes="x2 lit xs" version="1.0" xml:lang="en"

    Start numbering
    Numbering using a site map
    Numbering without a site map


19.1. Start numbering

The number template determines first the target node exists in the site map, and if so, invokes the number-from-site-map processing mode on the corresponding item in the site map. If it does not exist, it invokes the number processing mode on the target node.

These templates are recursively called, proceeding upwards towards the root. The built-in recursive functionality of xsl:number is not used, since that does not allow us to finely control the number format at each hierarchical step.

xsl:template name="number"
      xsl:param name="target" select="."
      xsl:param name="absolute-id"
        xsl:call-template name="absolute-id"
          xsl:with-param name="target" select="$target"

        xsl:when test="count($site-map-doc) > 0"
          xsl:for-each select="$site-map-doc"
            xsl:variable name="e" select="key('site-items', $absolute-id)"
              xsl:when test="count($e) > 0"
                xsl:apply-templates select="$e" mode="number-from-site-map"
                xsl:apply-templates select="$target" mode="number"

          xsl:apply-templates select="$target" mode="number"


19.2. Numbering using a site map

The number-from-site-map mode works recursively upwards the tree to determine the numbering.

xsl:template match="node()" mode="number-from-site-map"
xsl:template match="xs:site-item" mode="number-from-site-map"
        xsl:apply-templates select=".." mode="number-from-site-map"

        xsl:number level="single" count="xs:site-item" format="1."

19.3. Numbering without a site map

xsl:template match="node()" mode="number"
xsl:template match="/" mode="number" priority="2.0"
xsl:template match="x2:section" mode="number"
        xsl:call-template name="number"
          xsl:with-param name="target" select=".."
        xsl:number level="single" count="x2:section" format="1."

Formatted using xhtml2to1 by Steve Cheng.