"C #" XSLT Transformation XML instance

Source: Internet
Author: User
Tags net xml xpath xsl xslt


"C #" XSLT Transformation XML instance

The product used ASP a few years ago, and then upgraded to. Net 1.1, and then to 2.0, it has always been useful for XSLT to transform XML to generate Web pages in a slightly sorted way.
XML file:
<?xml version= "1.0" encoding= "Utf-8"?>
<ric>
<catalog>
<book price= ">"
<author>kalen delaney</author>
<name>inside SQL Server 2000</name>
</book>
<book price= ">"
<author>ken henderson</author>
<name>the Guru ' s Guide to SQL Server architecture</name>
</book>
</catalog>
</ric> XSLT File:
<?xml version= "1.0" encoding= "Utf-8"?>
<xsl:stylesheet version= "1.0"
Xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform" >
<xsl:output method= "html" encoding= "Utf-8"/>
<xsl:template match= "/" >
<body>
<table cellpadding= "0" cellspacing= "0" border= "1" style= "BORDER-COLLAPSE:COLLAPSE;FONT-SIZE:14PX;" >
<tr>
<th>book name</th>
<th>Author</th>
<th>Price</th>
</tr>
<xsl:for-each select= "//ric/catalog/book" >
<tr>
<td>
<xsl:value-of select= "Name" ></xsl:value-of>
</td>
<td>
<xsl:value-of select= "Author" ></xsl:value-of>
</td>
<td>
<xsl:value-of select= "@price"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</xsl:template>
</xsl:stylesheet> note xsl:output, specifying the format of the output after the conversion, can be XML, HTML, text, or, if not specified, the default value is XML. In the following conversion example 2 code, if you do not specify this sentence, you can see that the beginning of the conversion result will contain <?xml version= "1.0" encoding= "utf-16"?>, because the default method value is XML. If your XSLT is trying to output HTML without specifying Xsl:output, the first line of HTML output will be the XML declaration. For detailed Xsl:output, refer to: Generate the HTML DOCTYPE element using Xsl:output.

Converted HTML view

Book Nameauthorprice
Inside SQL Server 2000Kalen Delaney75
The Guru ' s Guide to SQL Server Architectureken Henderson200

1. Parsing with MSXML COM objects.
Based on the way MSXML COM objects can be in all scripting languages, VB,. NET, such as in the client's JavaScript, you can also create MSXML COM objects to convert XML to HTML. The following example is in the. NET environment, this method is used in the
Using MSXML2;
Using System.Xml;
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
MSXML2. DOMDocument xmldoc = new DOMDocument ();
Xmldoc.async = false;
Xmldoc.load (Server.MapPath ("A.xml"));
MSXML2. DOMDocument xsldoc = new DOMDocument ();
Xsldoc.async = false;
Xsldoc.load (Server.MapPath ("a.xslt"));
Response.Write (Xmldoc.transformnode (Xsldoc));
}
}
2. Converting with a class of. NET XML namespaces
Using System.Xml;
Using SYSTEM.XML.XSL;
Using System.Xml.XPath;
Using System.Text;
Using System.IO;
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
VS2003
XmlDocument doc = new XmlDocument ();
Doc. Load (Server.MapPath ("A.xml"));
XPathNavigator navgator = Doc. CreateNavigator ();
StringWriter output = new StringWriter ();
XslTransform transform = new XslTransform ();
Transform. Load (Server.MapPath ("a.xslt"));
Transform. Transform (navgator, NULL, Output);
Response.Write (output. ToString ());
}
}

Using System.Xml;
Using SYSTEM.XML.XSL;
Using System.Xml.XPath;
Using System.Text;
Using System.IO;
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
VS2005, you can also get XPathNavigator objects by XmlDocument like the VS2003 below
The difference between VS2005 and 2003 is that XslTransform in 2003 is replaced by XslCompiledTransform, which is the product of optimizing XSL performance under 2005
StringWriter output = new StringWriter ();
XslCompiledTransform transform = new XslCompiledTransform ();
Transform. Load (Server.MapPath ("a.xslt"));
Transform. Transform (Server.MapPath ("A.xml"), null, output);
Response.Write (output. ToString ());
}
}
VS2005 below, you can also use a different method, that is, to generate a temporary file of HTML. Note that the call to transform. Transform () method, the a.html file does not exist, the Transform.transform () method automatically creates the file and outputs the converted results to this file.
VS2005
XslCompiledTransform transform = new XslCompiledTransform ();
Transform. Load (Server.MapPath ("a.xslt"));
Transform. Transform (Server.MapPath ("A.xml"), Server.MapPath ("a.html"));
Response.WriteFile (Server.MapPath ("a.html")); Also noteworthy, the above uses Stringwrite as the Transform method output, the output result encoding is UTF16, if you must make the output encoding for UTF8, please use the following method. This is estimated to be stringwrite from the transform () method after receiving the result and then converting to UTF16, or. NET implements the transform () method, the Xsl:output node's settings are overwritten with the StringWriter encoding property.
VS2005
XslCompiledTransform transform = new XslCompiledTransform ();
Transform. Load (Server.MapPath ("a.xslt"));
MemoryStream stream = new MemoryStream ();
Transform. Transform (Server.MapPath ("A.xml"), null, stream);
StreamReader reader = new StreamReader (stream, System.Text.Encoding.UTF8);
Response.WriteFile (reader.    ReadToEnd ()); VS2005 (Framework 2.0) is correctly worded below, reference: Generate the HTML DOCTYPE element using Xsl:output.

3. Download. Using XSLT to transform XML to generate Web pages, the process of downloading is very convenient.
Default.aspx is a simple example with no code on the server side.
<input type= "button" value= "Download" onclick= "window.open (' download.aspx ', ' Download ');"/>
<iframe id= "Download" name= "Download" src= "style=" display:none; ></iframe> download.aspx:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Download.aspx.cs" inherits= "Download"%> Download.aspx.cs:
Using System.Xml;
Using SYSTEM.XML.XSL;
Using System.Xml.XPath;
Using System.Text;
Using System.IO;
public partial class Download:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
String fileName = "Aaa_" + DateTime.Now.ToString ("YYYYMMDDHHMMSS") + ". xls";
String fullpath = Server.MapPath (fileName);
XmlTextWriter writer = new XmlTextWriter (FullPath, Encoding.UTF8);
XslCompiledTransform transform = new XslCompiledTransform ();
Transform. Load (Server.MapPath ("a.xslt"));
Transform. Transform (Server.MapPath ("A.xml"), null, writer);
Writer. Close ();
Response.Clear ();
Response.AddHeader ("Content-disposition", "attachment; Filename= "+ fileName);
Response.WriteFile (FullPath);
}
This way to download, is the use of Excel can open the HTML page (in fact, the file suffix can be changed to the name of. doc), if you save the file to the local, with a text editor to open, you can see, actually downloaded is an HTML page file.
There are many ways to download it, this way has its advantages and disadvantages. The advantage is that if you parse a Web page with XSLT, this is a convenient way to download it, but if you download more data than the file is larger (more than 1M or so later), it will be slow to open in Excel and Word. If you don't have any special formatting requirements for downloaded Excel, you can just download one recordset to Excel, you can use XSLT to convert the XML to a CVS-formatted output text string, and then save it as a CVS file, so it's fast to open in Excel.

Add 4: Convert XML directly on the client. This is the earliest use of the way, because the client situation is complex, in some cases there may be problems, such as the client IE version, patch not enough to cause MSXML version is too low, if the use of Simplified Chinese in traditional, Japanese and other operating systems may also be problematic.
<?xml version= "1.0" encoding= "Utf-8"?>
<?xml-stylesheet type= "text/xsl" href= "A.xslt"?>
<ric>
<catalog>
<book price= ">"
<author>kalen delaney</author>
<name>inside SQL Server 2000</name>
</book>
<book price= ">"
<author>ken henderson</author>
<name>the Guru ' s Guide to SQL Server architecture</name>
</book>
</catalog>
Content such as XML </ric> above can be dynamically exported, XSLT files can also be dynamic, such as using <?xml-stylesheet type= "text/xsl" href= "***.aspx?id=100"?>, etc.

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.