XSLT Transformation XML Summary

Source: Internet
Author: User
Tags net xml xpath xsl xslt
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 = "a" >
< author > Kalen Delaney </author >
< name > Inside SQL Server </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 = "/" >
< HTML >
< 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 >
</HTML >
</xsl:template >
</xsl:stylesheet > Note xsl:output, specify the format of the output after the conversion, which 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 Price
Name Author
Inside SQL Server 2000 Kalen Delaney 75
The Guru ' s Guide to SQL Server architecture Ken Henderson 200
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 =

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.