Working with xsl:choose in XSLT
(Page 1 of 6 )
This is the second article in a series focusing on developing XSLT-oriented applications using ASP.NET 2.0. Even though the series is based on ASP.NET 2.0, I use it only as a transformation engine. The entire focus will be on working with XSLT. You can use any transformation engine according to your requirements (such as Java or others).
A
downloadable zip file is available for this article.
In this article, I will be focusing on working with the “xsl:choose” construct available in XSLT. This article is heavily dependent on the first article of this series, especially when working with ASP.NET 2.0. If you come across any problems, I strongly suggest you go through my first article.
The entire solution (source code) for this article is available for free download (in the form of a zip). All the applications in this series have been developed using Microsoft Visual Studio 2005 Professional Edition on Microsoft Windows Server 2003 Standard Edition together with Microsoft SQL Server 2005 Developer Edition as the database. You can even directly copy and paste the code given in this article and save with extensions XML or XSL and work with your own XSL parsers/engines.
I didn’t really test any of the code in any other tools/IDEs/servers/editions/versions. If you have any problems, please feel free to post in the discussion area.
Setting up your environment
To work with this article, I set up a new database table called “emp” with the fields empno, ename, sal and deptno. I also inserted a few sample rows. Once you execute the following query using “getXMLContent” method:
select Empno,Ename,Sal,Deptno from dbo.emp
You are likely to receive the following XML upon the execution of above SELECT command:
<SQLData>
<Rows>
<Empno>1001</Empno>
<Ename>Jag</Ename>
<Sal>4400</Sal>
<Deptno>10</Deptno>
</Rows>
<Rows>
<Empno>1002</Empno>
<Ename>Chat</Ename>
<Sal>2800</Sal>
<Deptno>20</Deptno>
</Rows>
<Rows>
<Empno>1003</Empno>
<Ename>Winner</Ename>
<Sal>3700</Sal>
<Deptno>10</Deptno>
</Rows>
<Rows>
<Empno>1004</Empno>
<Ename>Dhan</Ename>
<Sal>5000</Sal>
<Deptno>20</Deptno>
</Rows>
</SQLData>
I also included a sample XSLT which works with the above XML document to test the accuracy of XML generation. The XSLT is 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 select="SQLData/Rows/Ename" />
</xsl:template>
<xsl:template match="SQLData/Rows/Ename">
<xsl:value-of select="."/>
<br />
</xsl:template>
</xsl:stylesheet>
Once the above is executed, you should be given a list of all employee names. I also modified the code in such a way that it clearly lists the transformation. The following is the new code:
Dim xslt As New Xsl.XslCompiledTransform()
xslt.Load(Server.MapPath("XSLTFile11.xsl"))
' Create the writer.
Dim settings As New XmlWriterSettings()
settings.Indent = True
settings.IndentChars = ControlChars.Tab
settings.ConformanceLevel = ConformanceLevel.Auto
Dim sw As New IO.StringWriter
Dim writer As XmlWriter = XmlWriter.Create(sw, settings)
' Execute the transformation.
xslt.Transform(docXML, writer)
writer.Close()
Me.Literal1.Text = "<pre>" & Server.HtmlEncode(sw.ToString) & "</pre>"
sw.Close()
Next: Working with xsl:choose in XSLT: introduction >>
More ASP.NET Articles
More By Jagadish Chaterjee