Understanding XSLT Transformations: Matching and Selecting with Templates
This article mainly concentrates on the basics of “matching” and “selecting” different areas of XML and transforming those using different templates. This is the second part of the "Understanding XSLT Transformations" series. If you are new to the series, I strongly encourage you to go through the first article of this series.
Understanding XSLT Transformations: Matching and Selecting with Templates (Page 1 of 4 )
A downloadable file for this article is available here.
You can test all of the examples of this article with the utility I enclosed in my first article (developed using Visual Studio 2005). Remember that the utility (or the “exe”) works only on computers where .NET Framework 2.0 is installed (.NET framework 2.0 is freely downloadable from the Microsoft website).
Matching with “root” and selecting with “current context”
This example mainly contains two files. The first would be an XML file (Sample2.xml) and the second would be an XSL file (Sample2_1.xsl). Let us go through the “Sample2.xml” first.
I developed the above XML only for testing with several scenarios. So, I ask that you not worry about the proper XML structure conformances. Now, let us look at “Sample2_1.xsl”:
<?xml version="1.0" encoding="utf-8"?>
The most important statements of the whole XSL are the following:
The matching is based on “root node” which is “/”. The selection is based on “.”. The “dot” refers to the current context, which finds “Names” from the “root node”. The “dot” retrieves all the text of the current node as well as all nested nodes of the same. And thus you will have output something like the following: