Call template twice : call template « XSLT stylesheet « XML






Call template twice


File: Data.xml

<chapter>
  <title>Chapter 1</title>
  <para>para1</para>
  <para author="ar">para2</para>
  <section>
    <title>Chapter 1, Section 1</title>
    <para>para3</para>
    <para>line 1</para>
  </section>
</chapter>

File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:template name="titles">
    <xsl:param name="headerElement">h4</xsl:param>
    <xsl:element name="{$headerElement}">
      <xsl:apply-templates />
    </xsl:element>
  </xsl:template>

  <xsl:template match="chapter/title">
    <xsl:call-template name="titles">
      <xsl:with-param name="headerElement">h1</xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="section/title">
    <xsl:call-template name="titles">
      <xsl:with-param name="headerElement" select="'h2'" />
    </xsl:call-template>
  </xsl:template>

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

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

</xsl:stylesheet>

Output:

<html>
   <body>
        
      <h1>Chapter 1</h1>
        
      <p>para1</p>
        
      <p>para2</p>
        
          
      <h2>Chapter 1, Section 1</h2>
          
      <p>para3</p>
          
      <p>line 1</p>
        
      
   </body>
</html>

 








Related examples in the same category

1.Call template with parameters
2.template that does the replacement
3.One template calls another template
4.call-template with parameter