在asp.net 2.0中,xslt方面有如下的转变和新功能:
·xslcompiledtransform - 实际上是.net 1.0的 xsltransform ,但提供了更好的性能支持,也支持之前.net 1.0下的应用的顺利迁移.
·xsltargumentlist - 允许向xslt中传递参数或者对象
xsltcompileexception - 当通过loa()方法加载xsl文档时发生错误时产生的异常。
xsltexception - 当在对xsl文档进行解析时发生错误时产生的异常。
先来看个简单的例子,该例子从northwind数据库中拿出数据,以xml格式展示,再以xslt格式转换,其中xslt代码如下:
| <?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="html" /> <xsl:template match="/"> <html> <head> <title>simple xslt transformation</title> </head> <body> <h2>simple xslt transformation</h2> <table border="1" cellspacing="1" cellpadding="1"> <center> <xsl:for-each select="//categories"> <!-- each record on a seperate row --> <xsl:element name="tr"> <xsl:element name="td"> <xsl:value-of select="productsubcategoryid" /> </xsl:element> <xsl:element name="td"> <xsl:value-of select="name" /> </xsl:element> <xsl:element name="td"> <xsl:attribute name="align">center</xsl:attribute> <xsl:value-of select="modifieddate" /> </xsl:element> </xsl:element> </xsl:for-each> </center> </table> </body> </html> </xsl:template> </xsl:stylesheet> |
然后其展示的aspx代码为:
| <%@ page language="c#" %> <%@ import namespace="system.data.sqlclient" %> <%@ import namespace="system.xml" %> <%@ import namespace="system.xml.xsl" %> <%@ import namespace="system.xml.xpath" %> <%@ import namespace="system.web.configuration" %> <script runat="server"> void page_load(object sender, system.eventargs e) { string connstring = webconfigurationmanager.connectionstrings ["adventureworks"].connectionstring; using (sqlconnection connection = new sqlconnection(connstring)) { connection.open(); sqlcommand command = new sqlcommand ("select * from production.productsubcategory as categories " + " for xml auto,elements", connection); xmlreader reader = command.executexmlreader(); xpathdocument xpathdoc = new xpathdocument(reader); string xslpath = server.mappath("category.xsl"); xslcompiledtransform transform = new xslcompiledtransform(); transform.load(xslpath); transform.transform(xpathdoc, null, response.output); } } </script> |
其中注意我们先用xmlreader读取数据库提出来的数据(以xml auto的方式),然后载入xsl文件,再用xslcompiledtransform类进行转换,其中用xpathdocument是为了性能的提升。注意这里用xslcompiledtransform取代了.net 1.1中的xslttransform,运行结果如下图
![]() |
还可以向xslt中传入参数或对象,先看如何向其传入参数,比如要改变上例的背景颜色,则可以这样写xslt
| <?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="html" /> <xsl:param name="backgroundcolor" select="blue" /> <xsl:template match="/"> <html> <head> <title>passing parameters to an xslt style sheet</title> </head> <body> <h2> passing parameters to an xslt style sheet</h2> <table border="1" cellspacing="1" cellpadding="1"> <center> <xsl:for-each select="//categories"> <!-- each record on a seperate row --> <xsl:element name="tr"> <xsl:attribute name="bgcolor"> <xsl:value-of select="$backgroundcolor" /> </xsl:attribute> <xsl:element name="td"> <xsl:value-of select="productsubcategoryid" /> </xsl:element> <xsl:element name="td"> <xsl:value-of select="name" /> </xsl:element> <xsl:element name="td"> <xsl:attribute name="align">center</xsl:attribute> <xsl:value-of select="modifieddate" /> </xsl:element> </xsl:element> </xsl:for-each> </center> </table> </body> </html> </xsl:template> </xsl:stylesheet> |
要注意的是其中的是:
| <xsl:attribute name="bgcolor"> <xsl:value-of select="$backgroundcolor" /> |
以这样的形式指定了backgroundcolor是一个参数,而在xslt的一开始,以<xsl:param name="backgroundcolor" select="blue" />的方式,为backgroundcolor设定了一个值为蓝色,这样则为使<tr>的背景颜色bgcolor=blue,实现将输出数据的每一行变为蓝色的效果。
文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!





