用xslt解析xml
|
|||||||||||
用xslt解析xml | |||||||||||
作者:duckveed 文章来源:http://duckveed.spaces.live.com/PersonalSpace.aspx 点击数: 74 更新时间:2006-8-11 | |||||||||||
xml文件(a.xml)
<?xml version="1.0" encoding="utf-8" ?>
<ric> <catalog> <book price="75"> <author>Kalen Delaney</author> <name>Inside SQL Server 2000</name> </book> <book price="200"> <author>Ken Henderson</author> <name>The Guru's Guide to SQL Server Architecture</name> </book> </catalog> </ric> xsl文件(a.xsl)
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <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>
解析后的页面示例图
1. 用MSXML COM对象解析
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.xsl")); Response.Write(xmldoc.transformNode(xsldoc)); } } 2. 用.Net的xml命名空间的类来解析
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")); XmlElement root = doc.DocumentElement; XPathNavigator navgator = root.CreateNavigator(); StringBuilder builder = new StringBuilder();
XmlWriter writer = XmlWriter.Create(builder); XslTransform transform = new XslTransform(); transform.Load(Server.MapPath("a.xsl")); transform.Transform(navgator, null, writer); writer.Close(); Response.Write(builder.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 StringBuilder builder = new StringBuilder(); XmlWriter writer = XmlWriter.Create(builder); XslCompiledTransform transform = new XslCompiledTransform(); transform.Load(Server.MapPath("a.xsl")); transform.Transform(Server.MapPath("a.xml"), null, writer); writer.Close(); Response.Write(builder.ToString());
} } VS2005下面,也可以用另外的方法,即生成HTML的临时文件
//VS2005
XslCompiledTransform transform = new XslCompiledTransform(); transform.Load(Server.MapPath("a.xsl")); transform.Transform(Server.MapPath("a.xml"), Server.MapPath("a.html")); Response.WriteFile(Server.MapPath("a.html")); 3. 下载。用xslt解析xml生成网页,下载的处理是很方便的。
default.aspx如下,只是个简单的示例,服务器端没有任何代码。
<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.xsl")); transform.Transform(Server.MapPath("a.xml"), null, writer); writer.Close(); Response.Clear(); Response.Buffer = true; Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); Response.WriteFile(fullPath); } } 这种方式下载,是利用了Excel可以打开html网页(其实将文件后缀名改成.doc也就可以用word打开),如果将文件存到本地,用文本编辑器打开,就可以看到,实际上下载下来的是一个html的页面文件。
下载的方式很多,这种方式有它的优点和缺点。优点是如果用xslt解析的网页,这种下载方式实现上很方便;缺点,如果下载的数据比较多文件比较大(接近2M之后),用Excel、Word打开时会很慢。
|