新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论XSL,XSLT,XSL-FO,CSS等技术
    [返回] 中文XML论坛 - 专业的XML技术讨论区XML.ORG.CN讨论区 - XML技术『 XSL/XSLT/XSL-FO/CSS 』 → [转帖]ASP.NET 2.0中XSLT的使用 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 3341 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: [转帖]ASP.NET 2.0中XSLT的使用 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     Qr 帅哥哟,离线,有人找我吗?
      
      
      威望:9
      等级:博士二年级(版主)
      文章:4392
      积分:29981
      门派:XML.ORG.CN
      注册:2004/5/15

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给Qr发送一个短消息 把Qr加入好友 查看Qr的个人资料 搜索Qr在『 XSL/XSLT/XSL-FO/CSS 』的所有贴子 访问Qr的主页 引用回复这个贴子 回复这个贴子 查看Qr的博客楼主
    发贴心情 [转帖]ASP.NET 2.0中XSLT的使用

    在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://news.newhua.com/Html/AspNet/2006-6/22/0662214573842256_11.shtml


       收藏   分享  
    顶(0)
      




    ----------------------------------------------
    没人帮忙,那就靠自己,自己才是最好的老师!本人拒绝回答通过站内短消息提出的问题!

    blog:http://Qr.blogger.org.cn

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/11/2 9:58:00
     
     fukui 帅哥哟,离线,有人找我吗?
      
      
      等级:大一(猛啃高等数学)
      文章:17
      积分:145
      门派:IEEE.ORG.CN
      注册:2006/11/12

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给fukui发送一个短消息 把fukui加入好友 查看fukui的个人资料 搜索fukui在『 XSL/XSLT/XSL-FO/CSS 』的所有贴子 引用回复这个贴子 回复这个贴子 查看fukui的博客2
    发贴心情 
    谢了,学习+收藏!

    ----------------------------------------------
    XML新人,请大家指教!

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/11/13 8:32:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 XSL/XSLT/XSL-FO/CSS 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2025/8/10 23:36:06

    本主题贴数2,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    125.000ms