Understanding XSLT Transformations: Matching and Selecting with Templates - What are the default templates?
(Page 4 of 4 )
This example mainly contains two files. The first would be an XML file (Sample2.xml), which is same as above and the second would be an XSL file (Sample2_6.xsl). Now, let us look at “Sample2_6.xsl”:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<table border="1">
<xsl:apply-templates select="Names"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="Name">
<tr>
<td>
<xsl:value-of select="."/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
Let us look at only the matches and selections from the above XSL.
<xsl:template match="/">
<xsl:apply-templates select="Names"/>
<xsl:template match="Name">
<xsl:value-of select="."/>
The first statement starts at the “root”. The second tries to apply any templates (if available) to all the elements available in “Names” (including itself). The third statement is a template, which accepts only “Name” element (which may be available again in the same path of “Names”). The fourth statement just displays the text available in “Name” element (along with all of its nested elements).
From the above paragraph, we can understand that the text available in every element of “Name” will be in separate “td”. But, what about the text for “Names”? No template exists for “Names” element. And then the “default template” comes into the scenario (which is hidden inside the parser). According to this “default template”, any element not matched with any template, the text would be directly carried out without any transformation.
Before execution, make sure that you modify the XSL file name (to “Sample2_6.xsl”) within the XML document. When you execute the above you will get the following output, which is different from all of the above:
<?xml version="1.0" encoding="utf-16"?>
<html>
<body>
<table border="1">
these are the names
<tr><td>Jag</td></tr>
<tr><td>Win</td></tr>
<tr><td>Dhan</td></tr>
<tr><td>Ram</td></tr>
</table>
</body>
</html>
From the above you can understand that the text in “Names” is directly carried out and the text in every “Name” element is enclosed within “TR” and “TD”.
A final touch!
If you are experiencing troubles, try to be always “specific”. Always work with proper “matching”, “selecting” and “templating”. For example, the following XSL (Sample2_7.xsl) may be reasonable for your criteria (but it is up to you).
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<table border="1">
<xsl:apply-templates select="Names/Name"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="Name">
<tr>
<td>
<xsl:value-of select="."/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
The above excludes the text of “Names” element.
Any comments, suggestions, ideas, improvements, bugs, errors, feedback etc. are highly appreciated at jag_chat@yahoo.com.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |