title: Displaying xml nodes dynamically and applying style to specific nodes using recursion
body: <p>Below is my xml file.</p>
  
  <pre><code>&lt;xml&gt;
  &lt;top&gt;
   &lt;main&gt;
      &lt;firstname&gt;John&lt;/firstname&gt;
      &lt;lastname&gt;John&lt;/lastname&gt;
      &lt;table&gt;&lt;/table&gt;
      &lt;chapter&gt;
         &lt;firstname&gt;Alex&lt;/firstname&gt;
         &lt;lastname&gt;Robert&lt;/lastname&gt;
         &lt;p&gt;Sample text chap&lt;/p&gt;
         &lt;figure name="f1.svg"&gt;&lt;/figure&gt;
         &lt;chapter&gt;
            &lt;firstname&gt;Rebec&lt;/firstname&gt;
            &lt;lastname&gt;&lt;/lastname&gt;
            &lt;p&gt;Sample text&lt;/p&gt;
            &lt;figure name="f2.svg"&gt;&lt;/figure&gt;
         &lt;/chapter&gt;
      &lt;/chapter&gt;
   &lt;/main&gt;
  &lt;/top&gt;
  &lt;/xml&gt;
  </code></pre>
  
  <p><strong>Desired output:</strong></p>
  
  <pre><code>&lt;bold&gt;John
  table
  &lt;bold&gt;Robert
  Sample text chap
  f1.svg
  &lt;bold&gt; Rebec
  Sample text
  f2.svg
  </code></pre>
  
  <p><strong>Explaination:</strong> I have written an xslt to do this. I need to fetch the xml nodes dynamically. I cannot write: xsl:apply-templates select='main/lastname'. Because my xml format could change anytime. 
  I have tried a logic to first fetch all the xml nodes using '$root/*'. Then if 'table' element is encountered, i use xsl:apply-templates select='current()[name() = 'TABLE']' and perform table creation operations.
  This works fine. I get the desired output but my figure elements only displays f1.svg at every place in the output. f2.svg is not shown.
  And how do I match only 'lastname' and make it bold?
  I want to make the code as generic/modular as possible so that it loops through all the elements of the xml tree and does some formatting on the specific nodes.
  Below is a recursive xslt. With this my data is getting repeated. I am writing recursive template because xslt is not sequential.</p>
  
  <p><strong>XSLT:</strong></p>
  
  <pre><code>&lt;xsl:call-template name="FetchNodes"&gt;
  &lt;xsl:with-param name="endIndex" select="$NumberOfNodes" /&gt;
  &lt;xsl:with-param name="startIndex" select="1" /&gt;
  &lt;xsl:with-param name="context" select="$root/*" /&gt;
  &lt;/xsl:call-template&gt;
  
  
  &lt;xsl:template name="FetchNodes"&gt;
      &lt;xsl:param name="endIndex" /&gt;
      &lt;xsl:param name="startIndex" /&gt;
      &lt;xsl:param name="context" /&gt;
      &lt;xsl:if test="$startIndex &amp;lt;= $endIndex"&gt;
             &lt;xsl:if test="$context[$startIndex][name() = 'table']""&gt;
                    &lt;xsl:apply-templates select="$context[$startIndex][name() = 'table']"" mode="table" /&gt;
        &lt;/xsl:if&gt;
              &lt;xsl:call-template name="FetchNodes"&gt;
          &lt;xsl:with-param name="endIndex" select="$endIndex" /&gt;
          &lt;xsl:with-param name="startIndex" select="$startIndex + 1"/&gt;
          &lt;xsl:with-param name="context" select="$context" /&gt;
      &lt;/xsl:call-template&gt;
  &lt;/xsl:if&gt;
  &lt;/xsl:template&gt;
  
      &lt;xsl:template match="node()" mode="table"&gt;   
         &lt;xsl:value-of select="node()" /&gt;
      &lt;/xsl:template&gt;
  </code></pre>
  
  <p>With the above xslt, something is incorrect in the xpath of apply templates. Output is not proper.<br>
  I want XSL FO output.                                   </p>
  
  <p>Can anybody suggest something?</p>