title: XSLT (numbering nested divs in TEI Simple)
body: <p>I am using TEI Simple and want to add numbering to headers as well as into TOC lines. I understand how it could be built but can’t reach the desired result across parts of a book (<code>front</code>, <code>body</code>, <code>back</code>). It always restarts at the root. My code for two levels (with the rest I do the same):</p>
  
  <pre><code>&lt;xsl:template match="tei:div[@n='1']/tei:head" mode="toc"&gt;
    &lt;fo:block font-size="12pt"&gt;
      &lt;xsl:number format="1. " count="tei:div[@n='1']" level="multiple"/&gt;
      &lt;xsl:apply-templates/&gt;
    &lt;/fo:block&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="tei:div[@n='2']/tei:head" mode="toc"&gt;
    &lt;fo:block font-size="12pt"&gt;
      &lt;xsl:number format="1.1. " count="tei:div[@n='1']|tei:div[@n='2']" level="multiple"/&gt;
      &lt;xsl:apply-templates/&gt;
    &lt;/fo:block&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>Apparently, I mark any div with <code>n</code> attribute. I apply the template for the whole <code>text</code> part of the file. The result looks like this:</p>
  
  <pre><code>[Front]
  1. First
    1.1. First of First
  [Body]
  1. Second
  </code></pre>
  
  <p>The precise XPaths are:</p>
  
  <pre><code>TEI/text/front/div[@n="1"]/head
  TEI/text/body/div[@n="1"]/head
  TEI/text/back/div[@n="1"]/head
  </code></pre>
  
  <p>… and, obviously:</p>
  
  <pre><code>TEI/text/front/div[@n="1"]/div[@n="2"]/head
  TEI/text/body/div[@n="1"]/div[@n="2"]/head
  TEI/text/back/div[@n="1"]/div[@n="2"]/head
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have decided for a bit more strict book structure. Typically, it is not common to number front and back matter. Now I am using this and it works properly (I guess it could be possible to mix everything together for all main parts but this way is more reasonable for me. I use it for 8 levels.) <em>However, I wonder if there is another, much smarter way of doing this. The code seems a bit clumsy with increasing levels.</em></p>
  
  <pre><code>&lt;xsl:template match="tei:div[@n='1']/tei:head[ancestor::tei:front and ancestor::tei:back]" mode="toc"&gt;
    &lt;xsl:variable name="level" select="count(ancestor-or-self::tei:div)"/&gt;
    &lt;fo:inline&gt;
      &lt;xsl:apply-templates/&gt;
    &lt;/fo:inline&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="tei:div[@n='1']/tei:head[ancestor::tei:body]" mode="toc"&gt;
    &lt;xsl:variable name="level" select="count(ancestor-or-self::tei:div)"/&gt;
    &lt;fo:inline&gt;
      &lt;xsl:number format="1. " count="tei:div[@n='1'][ancestor::tei:body]" level="multiple"/&gt;
      &lt;xsl:apply-templates/&gt;
    &lt;/fo:inline&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="tei:div[@n='2']/tei:head[ancestor::tei:body]" mode="toc"&gt;
    &lt;xsl:variable name="level" select="count(ancestor-or-self::tei:div)"/&gt;
    &lt;fo:inline&gt;
      &lt;xsl:number format="1.1. " count="tei:div[@n='1'][ancestor::tei:body]|tei:div[@n='2'][ancestor::tei:    body]" level="multiple"/&gt;
      &lt;xsl:apply-templates/&gt;
    &lt;/fo:inline&gt;
  &lt;/xsl:template&gt;
  </code></pre>