All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
ASP.NET中设置默认提交按钮的代码_[Asp.Net教程]
11种php编程典型安全隐患及处理_[PHP教程]
C#技巧:给datagrid的按钮列添加css_[Asp.Net教程]
由浅入深学习动态网页制作PHP的编程与应用_php资料_编程技术
ASP.NET,2.0中直接将Access数据库导入到Excel文件中_[Asp.Net教程]
IIS运行不了ASP.NET的解决办法_[Asp.Net教程]
飘浮广告的显示脚本类(VBS,JS双版)_JavaScript技术_编程技术
WML Script标准函数库_[XML教程]
在ASP.net中从SQL数据库保存取出图片(可用于上传图片)
HOW,TO—操作定长字符串_[Asp.Net教程]
Reading number is top 10 pictures
关于提肛的健身效果
中国女孩大胆自拍,显露完美身材3
美女就是美女
漂亮的跳舞妹妹2
这玉米,买还是不卖?
So beauty, will let you spray blood4
治疗多发性骨髓瘤的特效药,一万二一支
Look for from human art net, is good--3
怀春少女-石一伊
福利福利。。。。。。
Download software ranking
Unix video tutorial10
Boxer Classic video3
Tram sex maniac 2 (H) rar bag8
美女写真2
Boxer's Top ten classic battle9
艳兽都市
The Bermuda triangle1
美女游泳记
Boxer vs Yellow5
Ashlynn Video4
delv published in(发表于) 2014/1/6 8:47:57 Edit(编辑)
ASP.NET,2.0中XSLT的使用_[Asp.Net教程]

ASP.NET,2.0中XSLT的使用_[Asp.Net教程]

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



作者:廖煜嵘编译 来源:天极开发





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.