|
在asp.net 2.0中,對XML的應(yīng)用大為增強,而在XSLT處理方面,也提供了新的功能。本文將簡單對asp.net 2.0中XSLT的使用作簡單的說明,當(dāng)然本文假定讀者有一定的XSLT的基礎(chǔ)知識。
在asp.net 2.0中,XSLT方面有如下的轉(zhuǎn)變和新功能:
·XslCompiledTransform - 實際上是.NET 1.0的 XslTransform ,但提供了更好的性能支持,也支持之前.net 1.0下的應(yīng)用的順利遷移.
·XsltArgumentList - 允許向XSLT中傳遞參數(shù)或者對象
XsltCompileException - 當(dāng)通過loa()方法加載XSL文檔時發(fā)生錯誤時產(chǎn)生的異常。
XsltException - 當(dāng)在對XSL文檔進行解析時發(fā)生錯誤時產(chǎn)生的異常。
先來看個簡單的例子,該例子從NORTHWIND數(shù)據(jù)庫中拿出數(shù)據(jù),以XML格式展示,再以XSLT格式轉(zhuǎn)換,其中XSLT代碼如下:
<?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www./1999/XSL/Transform"> <xsl:output method="html" /> <xsl:template match="/"> <HTML> <HEAD> ?。糡ITLE>Simple XSLT Transformation</TITLE> </HEAD> <BODY> <H2>Simple XSLT Transformation</H2> ?。紅able border="1" cellSpacing="1" cellPadding="1"> <center> ?。紉sl:for-each select="http://Categories"> ?。?-- Each record on a seperate row --> ?。紉sl:element name="tr"> ?。紉sl:element name="td"> ?。紉sl:value-of select="ProductSubcategoryID" /> ?。?xsl:element> <xsl:element name="td"> ?。紉sl:value-of select="Name" /> ?。?xsl:element> ?。紉sl:element name="td"> <xsl:attribute name="align">center</xsl:attribute> ?。紉sl: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讀取數(shù)據(jù)庫提出來的數(shù)據(jù)(以xml auto的方式),然后載入xsl文件,再用xslcompiledtransform類進行轉(zhuǎn)換,其中用xpathdocument是為了性能的提升。注意這里用xslcompiledtransform取代了.net 1.1中的xslttransform,運行結(jié)果如下圖
運行結(jié)果
|
還可以向XSLT中傳入?yún)?shù)或?qū)ο螅瓤慈绾蜗蚱鋫魅雲(yún)?shù),比如要改變上例的背景顏色,則可以這樣寫XSLT
<?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www./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="http://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是一個參數(shù),而在XSLT的一開始,以<xsl:param name="BackGroundColor" select="Blue" />的方式,為backgroundcolor設(shè)定了一個值為藍色,這樣則為使<tr>的背景顏色bgcolor=blue,實現(xiàn)將輸出數(shù)據(jù)的每一行變?yōu)樗{色的效果。
當(dāng)然,在上面的例子中,我們是已硬編碼的方式設(shè)置xslt的參數(shù),一般來說,應(yīng)該在asp.net 頁面中進行設(shè)置。而在asp.net 2.0中,可以使用XsltArgumentList類來向XSLT中傳遞參數(shù),具體使用方法如下:
<%@ 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("App_Data/Category.xsl"); XslCompiledTransform transform = new XslCompiledTransform(); transform.Load(xslPath); XsltArgumentList argsList = new XsltArgumentList(); string backGroundColor = "Tan"; //Add the required parameters to the XsltArgumentList object argsList.AddParam("BackGroundColor", "", backGroundColor); transform.Transform(xpathDoc, argsList, Response.Output); } } |
其中,注意黑體加粗部分,先實例化了XsltArgumentList類,接著設(shè)置了backGroundColor顏色,再使用XsltArgumentList類的addParam方法,向XSLT中原先設(shè)置好的BackGroundColor傳遞參數(shù)。最后,在XslCompiledTransform的transform方法中,其中的第二個參數(shù),傳入剛才實例化后的argsList,這樣就可以實現(xiàn)在aspx頁面中動態(tài)向XSLT中傳遞進參數(shù)了,實現(xiàn)的效果如下圖所示
實現(xiàn)的效果
|
除此之外,在.net 2.0中,還可以在xslt中直接調(diào)用外部的類中的方法。而被XSLT調(diào)用的外部類,我們稱為擴展對象。下面舉例說明,首先先建立一個類DateTimeConverter,這個類中,我們有一個方法可以將指定的日期進行格式化,如下所示:
using System; public class DateTimeConverter { public DateTimeConverter() {} public string ToDateTimeFormat(string data, string format) { DateTime date = DateTime.Parse(data); return date.ToString(format); } } |
將這個類放在App_Code這個文件夾下,以方便調(diào)用。為了在XSLT中調(diào)用這個類,首先在XSLT文件的開頭用XMLNS的方式指定要調(diào)用的擴展對象,如下代碼所示:
<?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www./1999/XSL/Transform" xmlns:DateTimeConverter="urn:DateTimeConverter"> <xsl:output method="html" /> <xsl:param name="BackGroundColor" select="Blue" /> <xsl:template match="/"> <HTML> <HEAD> <TITLE>Invoking extension objects from an XSLT Style Sheet</TITLE> </HEAD> <BODY> <H2>Invoking extension objects from an XSLT Style Sheet</H2> <table border="1" cellSpacing="1" cellPadding="1"> <center> <xsl:for-each select="http://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="DateTimeConverter:ToDateTimeFormat (ModifiedDate, ‘F‘)" /> </xsl:element> </xsl:element> </xsl:for-each> </center> </table> </BODY> </HTML> </xsl:template> </xsl:stylesheet> |
在上面的代碼中,我們用<xmlns:DateTimeConverter="urn:DateTimeConverter">的方式,給要被調(diào)用的擴展對象命名為DateTimeConverter,以方便下面的調(diào)用。而為了將日期格式化,通過<xsl:value-of select="DateTimeConverter:ToDateTimeFormat (ModifiedDate, ‘F‘)" />的方式,調(diào)用了外部類DateTimeConverter中的ToDateTimeFormat的方法,注意這里是以類名:方法名(參數(shù)表)的形式表示。
接下來,在aspx頁面中,調(diào)用該XSLT的代碼如下
<%@ 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("App_Data/Category.xsl"); XslCompiledTransform transform = new XslCompiledTransform(); transform.Load(xslPath); XsltArgumentList argsList = new XsltArgumentList(); string backGroundColor = "Tan";
argsList.AddParam("BackGroundColor", "", backGroundColor);
DateTimeConverter converter = new DateTimeConverter(); argsList.AddExtensionObject("urn:DateTimeConverter", converter); transform.Transform(xpathDoc, argsList, Response.Output); } } </script> |
在上面的代碼中,要留意的是,首先實例化了DateTimeConverter類,然后通過XsltArgumentList的AddExtensionObject方法,增加其擴展對象,其中用"urn:DateTimeConverter"的方式,指明了其擴展對象的別名。運行的效果如下圖
運行的效果
|
|