Tutorials to .com

Tutorials to .com » Dotnet » Foundation » ASP.NET 2.0 in the use of XSLT

ASP.NET 2.0 in the use of XSLT

Print View , by: iSee ,Total views: 8 ,Word Count: 1240 ,Date: Sat, 22 Aug 2009 Time: 10:00 AM

In asp.net 2.0, the application of xml greatly increased, and in the XSLT processing, but also provides a new function. In this paper, a simple in asp.net 2.0 on the use of XSLT to make a brief description, of course, this article assumes that readers have some basic knowledge of XSLT.

In asp.net 2.0 in, XSLT has the following changes and new features:

XslCompiledTransform - in fact. NET 1.0 The XslTransform, but to provide support for better performance, but also to support before. Net 1.0 Applications under the smooth migration.

XsltArgumentList - allow to pass parameters in XSLT or object

XsltCompileException - when through the loa () method to load XSL file when an error occurs when abnormal.

XsltException - When analysis of the XSL document when an error occurs when abnormal.

First look at a simple example, the example from the NORTHWIND database out of data to xml format and then format conversion XSLT, XSLT code which is as follows:

<? 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 and then display its code as follows:

<% @ 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>

One note, we first put forward xmlreader to read the database data (in the xml auto mode), and then loading xsl file, and then convert xslcompiledtransform category, which is to use xpathdocument enhance performance. Note here xslcompiledtransform replaced. Net 1.1 in xslttransform, the results are as follows plans


Can also be imported to the XSLT parameters or in the object, look at how its incoming parameters, such as the above example to change the background color, you can write 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>

It should be noted that one is:

<xsl:attribute Name="bgcolor">
<xsl:value-of Select="$BackGroundColor" />

This way is a backgroundcolor specified parameters, and in the beginning of XSLT to <xsl:param name="BackGroundColor" select="Blue" /> manner set backgroundcolor a value of the blue, so <tr> for the background color so that bgcolor = blue, the realization of the output data of each line into the effect of the blue.


.NET foundation Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.