Understanding XSLT Transformations: Matching and Selecting with Templates - Matching with the particular node and selecting with "current context"
(Page 2 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_2.xsl). Now, let us now look at “Sample2_2.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="Names">
<html>
<body>
<b>
<xsl:value-of select="."/>
</b>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The most important statements of the whole XSL are the following:
<xsl:template match="Names">
…
<xsl:value-of select="."/>
…
</xsl:template>
The matching is based on the “Names” node. The selection is based on “.”. The “dot” refers to the current context, which finds all the elements from the node “Names”. It finally retrieves all the text of the current node as well as all nested nodes of the same.
Before execution, make sure that you modify the XSL file name (to “Sample2_2.xsl”) within the XML document. When you execute the above you will get an exact output as shown in the previous section. This is because all of the text in all of the elements related to the “Names” node is the same!
Matching with “root” and selecting with “element path”
This example mainly contains two files. The first would be an XML file (Sample2.xml), which is the same as above, and the second would be an XSL file (Sample2_3.xsl). Now, let us look at “Sample2_3.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>
<b>
<xsl:value-of select="Names"/>
</b>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The most important statements of the whole XSL are the following:
<xsl:template match="/">
…
<xsl:value-of select="Names"/>
…
</xsl:template>
Now, the matching is again based on the “root” node. The selection is based on “Names”. The “Names” refers to all “Names” elements under the “root” node. It finally retrieves all the text of the current element “Names” as well as all nested nodes of the same.
Before execution, make sure that you modify the XSL file name (to “Sample2_3.xsl”) within the XML document. When you execute the above you will get an exact output as shown in first section. This is because all of the text in all of the nested elements related to “Names” element (along with its own text) is the same!
Next: Matching with “root”, “particular node” and selecting in different ways >>
More XML Articles
More By Jagadish Chaterjee