top
Loading...
ASP.NET2.0中XSLT的使用
教程推薦
·ASP.NET初學者入門實踐
·Visual Baisc.NET入門
·基于C#的接口基礎教程
·Visual Studio 2005
精彩專題
·ASP.NET創建XML Web服務
·Visual Basic 9.0新功能
·VB2005實現RSS覽盡天下事
主題社區
·ASP.NET源碼·ASP.NET

在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>

然后其展示的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,實現將輸出數據的每一行變為藍色的效果。

軟件頻道精品推薦

更多精彩
點擊體驗>>

當然,在上面的例子中,我們是已硬編碼的方式設置xslt的參數,一般來說,應該在asp.net 頁面中進行設置。而在asp.net 2.0中,可以使用XsltArgumentList類來向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";
//Add the required parameters to the XsltArgumentList object
argsList.AddParam("BackGroundColor", "", backGroundColor);
transform.Transform(xpathDoc, argsList, Response.Output);
}
}

其中,注意黑體加粗部分,先實例化了XsltArgumentList類,接著設置了backGroundColor顏色,再使用XsltArgumentList類的addParam方法,向XSLT中原先設置好的BackGroundColor傳遞參數。最后,在XslCompiledTransform的transform方法中,其中的第二個參數,傳入剛才實例化后的argsList,這樣就可以實現在aspx頁面中動態向XSLT中傳遞進參數了,實現的效果如下圖所示

點擊放大此圖片

除此之外,在.net 2.0中,還可以在xslt中直接調用外部的類中的方法。而被XSLT調用的外部類,我們稱為擴展對象。下面舉例說明,首先先建立一個類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這個文件夾下,以方便調用。為了在XSLT中調用這個類,首先在XSLT文件的開頭用XMLNS的方式指定要調用的擴展對象,如下代碼所示:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/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="//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">的方式,給要被調用的擴展對象命名為DateTimeConverter,以方便下面的調用。而為了將日期格式化,通過<xsl:value-of select="DateTimeConverter:ToDateTimeFormat (ModifiedDate, 'F')" />的方式,調用了外部類DateTimeConverter中的ToDateTimeFormat的方法,注意這里是以類名:方法名(參數表)的形式表示。

接下來,在aspx頁面中,調用該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"的方式,指明了其擴展對象的別名。運行的效果如下圖

作者:http://www.zhujiangroad.com
來源:http://www.zhujiangroad.com
北斗有巢氏 有巢氏北斗