Developing XSLT-based Applications in ASP.NET 2.0 - Defining templates for particular tags
(Page 4 of 7 )
In the previous section, we observed that we can include any number of templates according to our requirements. Now, let us consider how we would extract only the “FirstName” (without any other data) and transform it using the template. The following code handles this task:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="SQLData/Rows/FirstName" />
</xsl:template>
<xsl:template match="FirstName">
<b>
<xsl:value-of select="."/>
</b>
<br />
</xsl:template>
</xsl:stylesheet>
You need to observe only the following statement from the above XSLT:
<xsl:apply-templates select="SQLData/Rows/FirstName" />
That statement particularly says that it needs to apply a separate template (if available) if it finds “FirstName” at the path “SQLData/Rows.”
In the above case, it searches for “FirstName” only at the path “SQLData/Rows.” Let us consider defining and applying a template for “FirstName” available at any path. You may need to modify the above code to the following to achieve the same:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="//FirstName" />
</xsl:template>
<xsl:template match="FirstName">
<b>
<xsl:value-of select="."/>
</b>
<br />
</xsl:template>
</xsl:stylesheet>
When you precede the path with “//” it reflects to “any path.”
Next: Working with all siblings available at a particular path >>
More ASP.NET Articles
More By Jagadish Chaterjee