Applying XSLT to XML Using ASP.NET - How to Transform More than One Row?
(Page 5 of 5 )
Till now we worked with only one record using XSLT. But what if we want more than one record to be transformed using XSLT? This section addresses those needs. We shall further enhance the example in previous section so that it can transform more than one order from ‘orders’ table at a time.
In the example given in previous section, the SELECT retrieves only one row (as it is filtered by orderID). We need to change it, so that it can retrieve more than one row. Make the modification to the above statement as following:
Dim docXML As XmlDocument = getXMLContent("select top 10 orderid,freight from orders ")
The above SELECT statement retrieves only first 10 orders (of course you can give any query you require). Now, we need to make few more changes to Format1.xslt file. Modify the Format1.xslt file so that it looks consistent with the following code:
<?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>
<table width="50%" cellspacing="0" cellpadding="0" style="font-family:verdana;font-size:X-Small" border="1">
<tr bgcolor="#336699"><th align="left"><font color="White">OrderID</font></th><th align="right"><font color="White">Freight</font></th></tr>
<xsl:for-each select="SQLData/Rows">
<tr><td align="left"><xsl:value-of select="orderid" /></td><td align="right"><xsl:value-of select="freight" /></td></tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Let us examine the modified code:
<xsl:for-each select="SQLData/Rows">
<tr><td align="left"><xsl:value-of select="orderid" /></td><td align="right"><xsl:value-of select="freight" /></td></tr>
</xsl:for-each>
‘<xsl:for-each>’ element can be used to select every XML element of a specified node set. It is quite similar to ‘for’ loop. As we are filtering to some extent using ‘<xsl:for-each>’ element, ‘<xsl:value-of>’ element should be given with more specific information excluding the filtering.
Press F5 to execute the web application and you should be able to see a table of 10 orders from database transformed using XSLT.
Closing Remarks
I suggest you to learn XPath language to design XSLT very effectively. As this article mainly focuses on basics of XSLT, I could not give any information on XPath. I examined these examples using Microsoft SQL Server 2000 Enterprise Edition and Visual Studio.NET 2003 Enterprise Architect. But all the above samples would definitely work with any database (provided you make modifications to the .NET data provider). And it is not at all necessary to have Visual Studio.NET to work with XSLT. You can also implement the in-line approach of ASP.NET to get the things worked out. By designing XSLT with a bit analysis, you can design templates for entire web site (for look and feel compatibility). These templates could be reused for new web applications without any hurdle. Complicated reports can also be designed using XSLT.
I leave it to the developers to further research and investigate the above examples in all the aspects, till it meets yours requirements. Any doubts, comments, suggestions, bugs, errors or feedback are welcomed 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. |