Tags

, , , , ,


Using <xsl:Apply-templates/>

Prerequisit: Please go through previous post “XSLT Startup

Using apply-templates enable us to use default templates. It also let us not to think about complex XPATH statements/ finding a complex XPATH pattern that works. Though using apply-templates cause your XSLT code to be larger but it let your code to be simple and easy to go through.

I use same example as in “XSLT-startup” post but just used another XSLT:

<?xml version="1.0" encoding="utf-8" ?>

<xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”&gt;

<xsl:template match=”/”>

<html>

<body>

<h1>Employee Listing</h1>

<xsl:apply-templates/>

</body>

</html>

</xsl:template>

<xsl:template match=”employee”>

<div style=”border: 1px dashed  #66CCFF”>

<h3>Employee Id:

<xsl:value-of select=”@employeeid”/>

</h3>

<xsl:apply-templates select=”firstname”/>

<xsl:apply-templates select=”lastname”/>

<xsl:apply-templates select=”homephone”/>

<xsl:apply-templates select=”notes”/>

</div>

</xsl:template>

<xsl:template match=”firstname”>

<b>First Name: </b><xsl:value-of select=”.”/>

<br/>

</xsl:template>

<xsl:template match=”lastname”>

<b>

Last Name:    </b> <xsl:value-of select=”.”/>

<br/>

</xsl:template>

<xsl:template match=”homephone”>

<b>

Home Phone:</b> <xsl:value-of select=”.”/>

<br/>

</xsl:template>

<xsl:template match=”notes”>

<b>

Remarks:</b> <xsl:value-of select=”.”/>

<br/>

</xsl:template>

</xsl:stylesheet>

Where XSLT starts with a entry templates where match=”/” and gradually apply templates that matches with the given XPATH statement for each templates.

Please notice that all the Apply-templates tags are within the <xsl:Stylesheet> tag.
Output:
applytemplates

Advertisements