在asp.net 2.0中,对xml的应用大为增强,而在xslt处理方面,也提供了新的功能。本文将简单对asp.net 2.0中xslt的使用作简单的说明,当然本文假定读者有一定的xslt的基础知识。
在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> |
| <%@ 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> |
| <?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> |
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器