Developing XSLT-based Applications in ASP.NET 2.0 - Separating templates from the root template
(Page 3 of 7 )
Let us consider the following XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="FirstName">
<b>
<xsl:value-of select="."/>
</b>
</xsl:template>
</xsl:stylesheet>
The above XSL simply transforms only the text available in the “FirstName” tag (at any location in the document). All the rest would be directly displayed without any transformation.
If you wanted to handle the “FirstName” separately from root, you can rewrite the above XSLT as follows:
<?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 />
</xsl:template>
<xsl:template match="FirstName">
<b>
<xsl:value-of select="."/>
</b>
</xsl:template>
</xsl:stylesheet>
The above includes both root and “FirstName” templates. According to the above XSLT, when it finds “FirstName,” it goes to the template defined outside the root template.
If you forget to provide the following statement:
<xsl:apply-templates />
it would be a blank template and nothing would get displayed (or transformed).
Next: Defining templates for particular tags >>
More ASP.NET Articles
More By Jagadish Chaterjee