xml
  1. xml-xsl-apply-template

XML xsl:apply-template

The xsl:apply-templates element is used to apply a set of templates to the current node or a selected node based on the matching templates.

Syntax

<xsl:apply-templates select="xpath expression" mode="template mode"/>

The xpath expression is used to select the nodes to which the templates will be applied. The mode attribute is optional and is used to apply templates based on the specified mode.

Example

Consider the following XML input:

<plant>
    <commonName>Tulip</commonName>
    <scientificName>Tulipa</scientificName>
    <zones>
        <zone>3</zone>
        <zone>4</zone>
        <zone>5</zone>
        <zone>6</zone>
        <zone>7</zone>
    </zones>
</plant>

The following XSLT code applies a set of templates to the <plant> element:

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

<xsl:template match="plant">
  <h1><xsl:value-of select="commonName"/></h1>
  <xsl:apply-templates select="zones"/>
</xsl:template>

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

<xsl:template match="zone">
  <li><xsl:value-of select="."/></li>
</xsl:template>

Output

<html>
  <body>
    <h1>Tulip</h1>
    <ul>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
      <li>7</li>
    </ul>
  </body>
</html>

Explanation

The XSLT code defines three templates:

  • A template that matches the root node (/) and generates an HTML document with a <body> element.
  • A template that matches the <plant> element and generates an <h1> element with the value of the <commonName> element. It also applies a set of templates to the <zones> element.
  • A template that matches the <zones> element and generates an unordered list (<ul>) element. It also applies a set of templates to the child nodes of the <zones> element.
  • A template that matches the <zone> element and generates a list item (<li>) element with the value of the current node.

The xsl:apply-templates element is used to apply the templates defined above to the current input node and its child nodes.

Use

The xsl:apply-templates element is used to apply a set of templates to selected nodes based on the matching templates. It is often used in conjunction with other XSLT core elements like xsl:template, xsl:for-each, and xsl:if to transform XML documents into different formats.

Important Points

  • The xsl:apply-templates element is used to apply a set of templates to the current node or selected nodes based on matching templates.
  • The xsl:apply-templates element must be used within a template or another xsl:apply-templates element.
  • The select attribute is used to select nodes for which templates will be applied. If the attribute is omitted, templates will be applied to the current node.
  • The mode attribute is used to apply templates based on the specified mode.
  • If no matching template is found, the default template will be used.

Summary

The xsl:apply-templates element is a core element of XSLT used to apply a set of templates to the current node or selected nodes based on matching templates. It is a powerful tool for transforming XML documents into different formats and is often used in conjunction with other XSLT core elements.

Published on: