首页 > 编程语言 >ASP.NET 2.0中XSLT的使用

ASP.NET 2.0中XSLT的使用

时间:2023-08-02 21:35:53浏览次数:51  
标签:ASP name XSLT System element select NET 2.0 xsl


最近学习asp.net 2.0中的XSLT使用,发现有新功能,故编译了一篇文章,介绍之,原文发表在


http://dev.yesky.com/msdn/375/2453875.shtml这里,今放到这里



在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中传递进参数了,实现的效果如下图所示

<v:shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"> 
<v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:cnotallow="rect" gradientshapeok="t" o:extrusinotallow="f"></v:path><o:lock aspectratio="t" v:ext="edit"></o:lock></v:shapetype>


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

标签:ASP,name,XSLT,System,element,select,NET,2.0,xsl
From: https://blog.51cto.com/u_14230175/6943230

相关文章

  • WEB 2.0是什么(转)
    【导语】BloggerDon在他的“WEB2.0概念诠释”一文中提到“Web2.0是以Flickr、Craigslist、Linkedin、Tribes、Ryze、Friendster、Del.icio.us、43Things.com等网站为代表,以Blog、TAG、SNS、RSS、wiki等社会软件的应用为核心,依据六度分隔、xml、ajax等新理论......
  • ASP.NET2.0中用Gridview控件操作数据
     小弟新写的一篇文章,上,因为没图,比较方便摘录如下:在ASP.NET2.0中,加入了许多新的功能和控件,相比asp.net1.0/1.1,在各方面都有了很大的提高。其中,在数据控件方面,增加了不少控件,其中的Gridview控件功能十分强大。在本文中,将探讨Gridview控件中的一些功能特性和用......
  • Fortinet检测命令控制——就是通过心跳,最短60s,最长1天的周期,检测偏离度0.2
    id:3255ec41-6bd6-4f35-84b1-c032b18bbfcbname:Fortinet-Beaconpatterndetecteddescription:|'IdentifiespatternsinthetimedeltasofcontactsbetweeninternalandexternalIPsinFortinetnetworkdatathatareconsistentwithbeaconing.A......
  • Go 语言中 net/http 标准库的初步使用
    1.概述Go中的net/http库提供了HTTP客户端和服务端的实现。也就是说net/http可以分为客户端和服务器端两部分,库中的结构和函数有些只支持客户端和服务器这两者中的一个,而有些则同时支持客户端和服务器,如下图所示:2.对web服务器的理解的示意图2.1.http服务器(只讨论......
  • Kubernetes主流网络插件介绍
    一、Flannel1.1简介Flannel由CoreOS研发,使用”虚拟网桥和veth设备”的方式为Pod创建虚拟网络接口,通过可配置的后端(backend)定义Pod间的通信网络。它支持基于VXLAN和UDP的Overlay网络,以及基于三层路由的Underlay网络。    对于每一个容器而言,在加入网络时,在每个节点创建一......
  • RS232转Profinet网关rs232串门转网门接法
    大家好,今天我要给大家带来一个很有意思的案例分享。你们猜猜,这回我们要用捷米的一款神奇的网关JM-RS485/232-PN做什么呢?没错,我们要把一台扫码枪设备通过这个RS232转PROFINET网关,接入到一台西门子S7-1200PLC的Profinet网络中。想象一下,一个看似普通的扫码枪,通过这个神奇的网关,就能......
  • RS232自由转Profinet网关rs232串门转网门接法
    你是否曾经遇到过这样的问题:如何在不编写复杂代码的情况下,将条形码数据上传到PLC?今天,我们将为你揭示一个简单的解决方案!让我们来看看这个神奇的组合:捷米的JM-RS485/232-PN(rs232转Profient网关)和锐码的TC8850型号多功能无线扫码枪。通过将它们连接起来,我们可以轻松地将现场条形......
  • asp.net core docker 部署
    1.添加Dockfile文件#Seehttps://aka.ms/containerfastmodetounderstandhowVisualStudiousesthisDockerfiletobuildyourimagesforfasterdebugging.FROMmcr.microsoft.com/dotnet/aspnet:7.0ASbaseWORKDIR/appEXPOSE80EXPOSE443COPY..ENTRYPOINT["......
  • RS232转Profinet网关rs232和rs485的区别
    在工业自动化领域,如何将扫码枪与PLC连接一直是一个重要的问题。而今天,我们将通过一个案例来展示如何通过RS232转Profinet网关,将X-9300扫码枪接入到PLC1200工业以太网总线上。在这个过程中,我们将会用到捷米的RS232自由协议转Profinet网关。1, 首先,我们需要了解RS232和Profinet两......
  • aspx导出数据方法
    1、导出txtprivatestaticvoidExport(DataTabletb,stringfileName){HttpContext.Current.Response.Clear();stringFileName=fileName+".txt";HttpContext.Current.Response.Buffer=true;HttpContext.Current.Respons......