Numbering Things: Collected Data and Enumeration

How to use the collected enumeration data to number things in output.

The collected data includes data specifically designed to make numbering (enumeration) easy. The enumeration structure reflects the entire publication (root map) and contains the minimum amount of structure and data needed to number things, generate navigation lists, resolved cross references, and so on. This includes all topics, figures and tables with titles, sections, examples, and all div-type elements (<bodydiv>, <sectiondiv>, <div>).

The XML structure of the enumeration data looks like this:
<mapdriven:collected-data xmlns:mapdriven=""
      <frontmatter xmlns=""
                   class="+ map/topicref pubmap-d/frontmatter "
         <inside-front-cover class="+ map/topicref pubmap-d/inside-front-cover "
            <title>Front Cover</title>
         <preface class="+ map/topicref pubmap-d/preface "
            <title>Copyright Page</title>

The <enum:enumerables> reflects the hierarchical structure of the publication. Each element reflects its DITA @class value, the URI of the source document it came from, and its "source ID", which is a unique identifier for the element constructed from its original context. The source ID is generated by the DITA utilities function df:generate-dita-id().

To number an element in output processing, you generate the source ID for the element you want to number, then apply templates to the element that has that ID within the enumerables data in the mode enumeration, as in this fragment for numbering figures:
  <xsl:variable name="sourceId" select="df:generate-dita-id(.)" as="xs:string"/>
  <span class="figcap">
    <xsl:apply-templates select="$collected-data/enum:enumerables//*[@sourceId = $sourceId]"
      <xsl:apply-templates select="*[contains(@class,' topic/title ')]" mode="figtitle"/>
In the mode enumeration you provide templates that implement your numbering rules, e.g.:
  <xsl:template mode="enumeration"  match="*[df:class(., 'topic/fig')][enum:title]">
    <!-- Context item should be enum:* from the collected-data 
         NOTE: Within the collected data, all titles are normalized
         to enum:title with no @class attribute.
    <span class="enumeration fig-enumeration">
      <xsl:text>Figure </xsl:text>      
      <xsl:number count="*[df:class(., 'topic/fig')][enum:title]"

Note that in this template, the usual <xsl:number> facility is used to number the figure. Because the enumeration data reflects the entire publication you can number figures across the entire publication, as in this example. Because the enumeration data also includes all the topic and topicheads in their map-defined hierarchy, you could also number things at a part, chapter, or other level, as long as it's provided for in the enumeration structure.