title: XSLT passing variables inside nested for-each loops
body: <p>I am fairly new at this and I am not sure if it is possible to do what I want to achieve here. I have 3 for-each loops that stores a number for every chapter, section and paragraph. I want to get that stored number from the previous for-each loop and display it in the nested loop, but I can't get it to work.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;xsl:stylesheet 
          version="1.0"
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  
      &lt;xsl:template match="/"&gt;
              &lt;html&gt;
                  &lt;head&gt;
                      &lt;title&gt;Table of Contents, Chapter 3&lt;/title&gt;
                  &lt;/head&gt;
  
                  &lt;body&gt;
                      &lt;xsl:for-each select="chapter"&gt;
                          &lt;tr&gt;
                              &lt;xsl:variable name="capnr"&gt;
                                  &lt;xsl:number value="3" format="1. "/&gt;
                              &lt;/xsl:variable&gt;
                              &lt;xsl:value-of select="$capnr"/&gt;
                              &lt;xsl:value-of select="@title"/&gt;
                          &lt;/tr&gt;
                          &lt;br /&gt;
  
                          &lt;xsl:for-each select="section"&gt;
                              &lt;tr&gt;
                                  &lt;xsl:variable name="secnr"&gt;
                                      &lt;xsl:number format="1. "/&gt;
                                  &lt;/xsl:variable&gt;
                                  &lt;xsl:value-of select="$capnr"/&gt;
                                  &lt;xsl:value-of select="$secnr"/&gt;
                                  &lt;xsl:value-of select="@title"/&gt;
                              &lt;/tr&gt;
                              &lt;br /&gt;
  
                              &lt;xsl:for-each select="paragraph"&gt;
                                  &lt;tr&gt;
                                      &lt;xsl:variable name="parnr"&gt;
                                          &lt;xsl:number format="1 "/&gt;
                                      &lt;/xsl:variable&gt;
                                      &lt;xsl:value-of select="$capnr"/&gt;
                                      &lt;xsl:value-of select="$secnr"/&gt;
                                      &lt;xsl:value-of select="$parnr"/&gt;
                                      &lt;xsl:value-of select="@title"/&gt;
                                  &lt;/tr&gt;
                              &lt;/xsl:for-each&gt;
                              &lt;br /&gt;
  
                          &lt;/xsl:for-each&gt;
  
                      &lt;/xsl:for-each&gt;
  
                  &lt;/body&gt;
  
              &lt;/html&gt;
  
          &lt;/xsl:template&gt;
  
  
      &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>XML:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;?xml-stylesheet type="text/xsl" href="ToC-3.xsl"?&gt;
  &lt;chapter title="Chapter 3: Expressions"&gt;
  
  &lt;section title="Variables"&gt;
  &lt;paragraph title="Simple variables"&gt;&lt;/paragraph&gt;
  &lt;paragraph title="Text variables"&gt;&lt;/paragraph&gt;
  &lt;paragraph title="Remote identifiers"&gt;&lt;/paragraph&gt;
  &lt;/section&gt;
  
  &lt;section title="The logical operators"&gt;
  &lt;paragraph title="Precedence of Boolean operators"&gt;&lt;/paragraph&gt;
  &lt;/section&gt;
  
  &lt;section title="Designational expressions"&gt;
  &lt;/section&gt;
  
  &lt;/chapter&gt;
  </code></pre>