除此之外,在.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"的方式,指明了其扩展对象的别名。运行的效果如下图
![]() |
- 相关新闻
- 用户评论
数据载入中,请稍后……
评论内容:不能超过100字,不需审核,请自觉遵守互联网相关政策法规。
- 推广服务
IT部落推荐阅读
·生活服务
·精彩图文
·赞助商链接
Rss订阅

