选择显示字体大小

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>

  要注意的是其中的是:


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons