ASP.NET 2.0中XSLT的使用

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"的方式,指明了其扩展对象的别名。运行的效果如下图

时间: 2024-08-31 20:02:43

ASP.NET 2.0中XSLT的使用的相关文章

ASP.NET 2.0中层次数据的处理

asp.net|数据 数据源控件可以同时暴露平面表格式的或层次的数据.前面演示的SqlDataSource和ObjectDataSource控件都是平面表格式的数据源控件.ASP.NET 2.0还包含两个层次数据源控件:用于连接XML文件的XmlDataSource和用于连接站点导航数据的SiteMapDataSource.这一部分将介绍这些控件的使用技术. TreeView和Menu控件 数据绑定控件与数据源控件类似,也可以是层次的.表格式数据绑定控件显示数据列表或表格,层次数据绑定控件能够用

在ASP.NET 2.0中建立站点导航层次(1)

站点导航提供程序--ASP.NET 2.0中的站点导航提供程序暴露了应用程序中的页面的导航信息,它允许你单独地定义站点的结构,而不用考虑页面的实际物理布局.默认的站点导航提供程序是基于XML的,但是你也可以通过编写自定义的提供程序,从任何后端位置暴露这些信息. 站点导航API--站点导航API用于在应用程序的代码中访问站点导航信息,它摘录了导航信息存储的细节.你可以使用API来编程访问应用程序的导航节点. 导航控件--导航控件为页面之间的导航提供了通用的UI,例如树视图.菜单和breadcrum

抢先试用ASP.NET 2.0中的新型安全控件

asp.net|安全|控件 一. 引言 与ASP.NET 2.0一同上市的有几个新的安全控件-它们位于工具的Login选项卡中(见图1)-这些控件大大简化了Web开发人员的工作.通过使用这些新的安全控件,现在你可以执行例如用户登录.注册.口令改变等的任务:而且,为此做出的努力仅是拖放相应的控件到你的Web表单上去.在本文中,我将向你展示怎样使用这些新控件来实现用户认证. 首先,让我们探索一下LoginView.LoginStatus和LoginName三个控件的使用.首先,让我们使用Visual

asp.net 2.0中的ValidationGroup

asp.net|asp.net 在asp.net 1.1中,对于验证类控件,在使用时,遇到有的不需要验证的控件时,是十分麻烦的,就是说不可能有选择验证某些控件,而在asp.net 2.0中,新增加了validationgroup属性,可以指定验证某些控件,例子如下: <html> <body> <form runat="server"> <asp:textbox id="TextBox1" runat="serve

ASP.NET 2.0中实现跨页面提交

asp.net|页面 在ASP.NET 1.X 版本中,页面都是提交到自己本身,并不能方便的指定需要提交的目的页面.例如FirstPage.aspx中的button只能提交到FirstPage.aspx,而不能提交到SecondPage.aspx.很多时候,ASP.NET 1.X这样工作方式使我们的开发方式受到不少限制.熟悉ASP/JSP/PHP的朋友大概很不习惯,因为以前经常使用的提交方式突然无法使用,虽然也有解决这个问题的方法(演示Webcast),可是过程太烦琐,不甚方便.令我们高兴的是,

ASP.NET 2.0 中配合 Master Page 使用的优化 CSS 模型

asp.net|css|优化 ASP.NET 2.0 中增加了内建的 MasterPage 的支持,这对我们来说是一个很大的便利.然而经过一段时间的使用,我发现 MasterPage 并不是那么完美:嵌套的 MasterPage 不能支持设计时界面,以及下面要提到的Content Page 中增加 CSS 的问题. 通常,在没有 2.0 之前,我们在页面里要增加一个 CSS 引用的语法如下: <link rel="stylesheet" href="css/test.c

理解并扩展 ASP.NET 2.0 中的站点导航系统

asp.net|导航|站点 摘要:ASP.NET 2.0 站点导航系统构建于一个功能强大.灵活的体系结构之上,设计这样的体系结构是为了使其具有可扩展性.本文探究站点提供程序的体系结构并提供一个示例提供程序,该提供程序将文件系统公开为站点导航的数据源,从而替代了标准的 Web.sitemap XML 文件. 简介 大多数 web 站点采用可视化导航的某种形式来帮助用户轻松地浏览站点,以及查找他们所需的信息和 Web 页.尽管不同站点之间的感观效果千差万别,但是通常会使用相同的基本元素 - 以导航栏

asp.net 2.0中tablecontrol搭配masterpage的小bug

asp.net|erp 在asp.net 2.0中,如果在一个masterpage页面中,使用服务端的table控件的话,如下所示,会在设置视图时,没了其中的contentplaceholder,     <asp:Table ID="tbl" runat="server">            <asp:TableRow>                <asp:TableCell>                    Th

ASP.NET 2.0 中的异步页功能应用

asp.net|异步 下载本文源代码:WickedCode0510.exe ASP.NET 2.0 提供了大量新功能,其中包括声明性数据绑定和母版页,成员和角色管理服务等.但我认为最棒的功能是异步页,接下来让我告诉您其中的原因. 当 ASP.NET 接收针对页的请求时,它从线程池中提取一个线程并将请求分配给该线程.一个普通的(或同步的)页在该请求期间保留线程,从而防止该线程用于处理其他请求.如果一个同步请求成为 I/O 绑定(例如,如果它调用一个远程 Web 服务或查询一个远程数据库,并等待调用