How to use xsl:apply-templates in XSLT

In XSLT you can create transform template for each element in the source document. When you use the template, you expand it and embed to somewhere in the destination document.

The xsl:apply-templates is an instruction in XSLT. It searches other templates defined in XSLT according to rules and expand the template then fill into where the instruction was defined.

Consider the following XML document, it's a table of contents.

 
<contents>
  <part title="Lucene Basics(or Fundamentals)">
    <chapter title="Lucene Searching">
      <section type="internal" title="Lucene Scoring">
        <leaf title="How Lucene scoring works" seotitle="how-lucene-scoring-works">
        </leaf>
      </section>    
      <section type="terminal" title="" seotitle="">
        <leaf title="hello world" seotitle="how-lucene-scoring-works">
        </leaf>
      </section>
    </chapter>
  </part>
 
  <part title="Lucene Index">
    <chapter title="Lucene Searching">
      <section type="internal" title="Lucene Scoring">
        <leaf title="How Lucene indexing works" seotitle="how-lucene-indexing-works">
        </leaf>
        <leaf title="Lucene Index tutorial" seotitle="lucene-index-tutorial">
        </leaf>
      </section>    
      <section type="terminal" title="" seotitle="">
      </section>
    </chapter>
  </part>  
</contents>
 

We will convert it into HTML tree structure.

Select leaf elements from all descendant of current node:

 
<xsl:template match="chapter">
                  <xsl:apply-templates select=".//leaf"/>
</xsl:template>
 

Select children of current node with element name "section" in template

 
<xsl:template match="chapter">
                  <xsl:apply-templates select="section"/>
</xsl:template>
 

Select all templates defined on all children of current node

 
<xsl:template match="chapter">
                  <xsl:apply-templates/>
</xsl:template>
 

The following XSLT illustrated several usages of xsl:apply-templates.

 
 <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:template match="/">
         <div class="toc-contents">
             <ul>
                 <xsl:apply-templates/>
             </ul>
         </div>
     </xsl:template>
     <xsl:template match="part">
         <li>
          <div class="toc-part">
              <h1><xsl:value-of select="@title"/></h1>
              <ul>
                  <xsl:apply-templates select="chapter"/>
              </ul>
          </div>
         </li>
     </xsl:template>
     <xsl:template match="chapter">
         <li>
          <div class="toc-chapter">
              <h2><xsl:value-of select="@title"/></h2>
              <ul>
                  <xsl:apply-templates select=".//leaf"/>
              </ul>
          </div>             
         </li>
     </xsl:template>
     <xsl:template match="leaf">
 
         <li>
             <a>
                 <xsl:attribute name="href">
                     http://makble.com/<xsl:value-of select="@seotitle"/>
                </xsl:attribute>
              <xsl:value-of select="@title"/></a>
         </li>
     </xsl:template>
 </xsl:stylesheet>
 

Displayed in HTML