Applying XSLT to XML Using ASP.NET - A Simple Example on XSLT
(Page 2 of 5 )
Just to understand the structure of XSLT, let us test a small example on XSLT using a simple XML document without using any server-side (ASP.NET) code. This example can be directly executed by opening the XML document in any XSLT supported browser (currently I tested with IE6).
So, let us start by creating a plain and simple XML document (sample1.xml) as follows.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="sample1.xslt"?>
<Employees>
<Employee>
<Name>Stuart</Name>
<Age>28</Age>
</Employee>
</Employees>
Now we create an XSL style sheet (sample1.xslt) for the above XML document as follows.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<b>Name of Employee : </b><xsl:value-of select="Employees/Employee/Name" /><br/>
<b>Age:</b><xsl:value-of select="Employees/Employee/Age" /><br/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Remember that the family of XML technologies are case sensitive. By this time, if you open “sample1.xml” directly in IE6 or NN6, you should be able to see the result something like the following:
Name of Employee: Stuart
Age: 28
If you observe the HTML source at browser, it would be something like the following, which is nothing but the transformation applied through XSLT to the XML document:
<html>
<body>
<b>Name of Employee : </b>Stuart<br>
<b>Age:</b>28<br>
</body>
</html>
Let us try to understand the above two files in detail now. “sample1.xml” is just a simple XML document. “sample1.xslt” is our style sheet definition to transform “sample1.xml”. If we carefully observe the sample1.xml file, we can spot an interesting line:
<?xml-stylesheet type="text/xsl" href="sample1.xslt"?>
This is the only magic needed in any XML file to be transformed to any XSLT. The above statement instructs browser to link the current XML document with “sample1.xslt” and present (transform) it accordingly. So, in general we will not see the XML document as a tree structure anymore. It will be presented (transformed) in a different way as specified in “sample1.xslt”.
Now, let us study the most important lines in sample1.xslt.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
The above line defines that it is basically an XSLT document used for certain transformation.
<xsl:template match="/">
The above line specifies the format (template) applicable to the root of XML document. We can also specify individual templates for each and every XML element as well. The attribute ‘match’ can be provided with any XPath expression.
<xsl:value-of select="Employees/Employee/Name" />
And finally, the above line states that it has to get the value (text) present in that path (Employees/Employee/Name) of XML document (tree) starting from root. In this case it starts from root (/), then searches for Employees element (first element), within that Employee child element (first child element) and finally the child element Name (first child element) of Employee element. This path could be replaced with any XPath expression.
The rest of the content should be easily understandable, as it mostly contains XHTML elements to be transformed. Make sure that this example always takes only the first elements into consideration. There is no use of placing more than one employee information in sample1.xml file. We will overcome this drawback in the later part of this article.
Next: XSLT with ASP.NET >>
More XML Articles
More By Jagadish Chaterjee