Open In App

XSLT <template> Element

Last Updated : 28 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

XSLT <template> element is a basic building block in XSLT and it provides a way to define rules for transforming specific elements into a source XML document.

It is an instruction that tells a browser how to transform XML into something else. It looks for specific things in the XML called patterns and decides what the changed part should look like in the end. It must be placed inside <xsl:stylesheet> or <xsl:transform>.

Attributes:

Attribute

Description

match

It specifies the pattern that an element in the source XML document must match for this template to be applied

name

You can assign a name to the template allowing it to be referenced elsewhere in the stylesheet

priority

You can specify the priority of the template. Templates with higher priorities are applied first.

mode

It defines the processing mode in which this template should be applied.

Syntax:

<xsl:template
match = "pattern"
name = "name"
priority = "number"
mode = "name">
<!-- Content -->
</xsl:template>

Example 1:

In this example we have a XML data bookstore which have a one book data with title and author value and we are accessing this data by using a match attribute in <xsl:template> and then we are accessing the value of title and author by using <xsl:value-of select=”key”/> Tag.

XML




<bookstore>
  <book>
    <title>XSLT "template" element</title>
    <author>Geeks For Geeks</author>
  </book>
</bookstore>


XML




<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <!-- It matches 'book' element -->
  <xsl:template match="book">
      
    <!-- It output HTML H3 element with value of title -->
    <h3> Title: <xsl:value-of select="title" /></h3>
      
    <!-- It output HTML H4 element with value of author -->
    <h4> Author: <xsl:value-of select="author" /></h4>
    
  </xsl:template>
</xsl:stylesheet>


Output:

xslt-template-example-1

Example 2:

In this example we have a two product XML data with their name and price and we are using <xsl:choose> block to choose a one value based on a condition provide in a <xsl:when test=”condition”> and if the condition is not satisfy then a <xsl:otherwise> part will be executed.

XML




<products>
  <product>
    <name>Car</name>
    <price>1000000</price>
  </product>
  <product>
    <name>Smartphone</name>
    <price>5000</price>
  </product>
</products>


XML




<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <!-- It matches 'product' element -->
  <xsl:template match="product">
    <h3> Product Information </h3>
    <p>
      <strong>Name: </strong> <xsl:value-of select="name" /> <br />
      <strong>Price: </strong> <xsl:value-of select="price" /><br />
      <strong>Affordable/Expensive: </strong>
        
      <!-- choose block to determine it is Affordable or Expensive: -->
      <xsl:choose>
          
        <!-- if the price is less than 20000 -->
        <xsl:when test="price < 20000">
          <span>Affordable</span>
        </xsl:when>
  
        <!-- otherwise (else part) -->
        <xsl:otherwise>
          <span>Expensive</span>
        </xsl:otherwise>
  
      </xsl:choose>
    </p>
  </xsl:template>
</xsl:stylesheet>


Output

xslt-template-example-2



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads