Use of XSLT in asp.net 2.0

Source: Internet
Author: User
Tags format new features string format xmlns xpath xsl xsl file connectionstrings
asp.net in asp.net 2.0, the application of XML is greatly enhanced, and in XSLT processing, new functionality is also provided. This article will briefly explain the use of XSLT in ASP.net 2.0, which, of course, assumes that the reader has some basic knowledge of XSLT.

In ASP.net 2.0, the XSLT aspect has the following transformations and new features:

· XslCompiledTransform-is actually a. NET 1.0 XslTransform, but provides better performance support, as well as a smooth migration of applications under. NET 1.0.

· XsltArgumentList-Allows parameters or objects to be passed to XSLT

XsltCompileException-The exception that is generated when an error occurs when an XSL document is loaded through the LOA () method.

XsltException-The exception that is generated when an error occurs while parsing the XSL document.

Let's take a look at a simple example, which takes data from the Northwind database, displays it in XML format, and converts it in XSLT format, where the XSLT code 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>
Simple <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>
The ASPX code that is then displayed is:

<%@ 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>
Note that we first use XmlReader to read data from the database (in XML auto), then load the XSL file, and then use the XslCompiledTransform class for the conversion, where the XPathDocument is to improve performance. Note that this replaces the xslttransform in. NET 1.1 with XslCompiledTransform, and the results of the operation are shown below


You can also pass parameters or objects to XSLT and see how to pass arguments to them, such as to change the background color of the previous example, so 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 a XSLT Style Sheet </TITLE>
</HEAD>
<BODY>
<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>
One of the things to be aware of is that:

<xsl:attribute name= "bgcolor"
<xsl:value-of select= "$BackGroundColor"/>


BackgroundColor is specified in this form as a parameter, and at the beginning of the XSLT, , A value of blue is set for BackgroundColor so that the background color is bgcolor=blue, and the effect of changing each row of the output data to blue is achieved.

Of course, in the example above, we set the XSLT parameters in a hard-coded way, which should normally be set in the ASP.net page. In asp.net 2.0, you can use the XsltArgumentList class to pass parameters to XSLT by using the following methods:

<%@ 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);
}
}
In which, note the bold bold section, first instantiate the XsltArgumentList class, then set the backgroundcolor color, and then use the XsltArgumentList class AddParam method, Set the BackgroundColor pass parameters to the XSLT Central Plains first. Finally, in the transform method of XslCompiledTransform, the second parameter, passing in the argslist just instantiated, enables you to pass the parameters to the XSLT dynamically in the ASPX page, as shown in the following illustration


In addition, in. NET 2.0, you can also call methods in an external class directly in XSLT. The outer class, which is called by the XSLT, is called an extension object. Here's an example of a class Datetimeconverter first, in which we have a way to format the specified date as follows:

Using System;
public class Datetimeconverter
{
Public Datetimeconverter ()
{}
public string Todatetimeformat (string data, string format)
{
DateTime date = datetime.parse (data);
Return date. ToString (format);
}
}
Place this class under the App_Code folder for easy invocation. To invoke this class in XSLT, first specify the extension object to invoke in the beginning of the XSLT file, as shown in the following code:

<?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 a XSLT Style Sheet </TITLE>
</HEAD>
<BODY>
<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>
In the code above, we use the <xmlns:datetimeconverter= "Urn:datetimeconverter" to name the extended object to be called Datetimeconverter to facilitate the invocation below. and in order to format the date, through the <xsl:value-of select= "Datetimeconverter:todatetimeformat (modifieddate, ' F ')"/> Way, Call the method of the Todatetimeformat in the outer class datetimeconverter, note that this is represented as a class name: Method name (parameter table).

Next, in the ASPX page, the code to invoke the XSLT is 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 ("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>
In the above code, note that the Datetimeconverter class is instantiated first, and then the extended object is incremented by the XsltArgumentList AddExtensionObject method, which uses the "Urn:d Atetimeconverter, which indicates the alias of its extended object. The effect of running the following figure



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.