用xslt解析xml

marginwidth="0" marginheight="0" src="http://www.aiwo1314.com/googleAdsense/gd468_60.html" frameborder="0" width="468" scrolling="no" height="60" leftmargin="0" topmargin="0">
 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>
marginwidth="0" marginheight="0" src="http://www.aiwo1314.com/googleAdsense/gd468_60.html" frameborder="0" width="468" scrolling="no" height="60" leftmargin="0" topmargin="0">
 
    解析后的页面示例图
Book NameAuthorPrice
Inside SQL Server 2000Kalen Delaney75
The Guru's Guide to SQL Server ArchitectureKen Henderson200
 
    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());
    }
}
marginwidth="0" marginheight="0" src="http://www.aiwo1314.com/googleAdsense/gd468_60.html" frameborder="0" width="468" scrolling="no" height="60" leftmargin="0" topmargin="0">
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;
marginwidth="0" marginheight="0" src="http://www.aiwo1314.com/googleAdsense/gd468_60.html" frameborder="0" width="468" scrolling="no" height="60" leftmargin="0" topmargin="0">
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打开时会很慢。


marginwidth="0" marginheight="0" src="http://www.aiwo1314.com/googleAdsense/gd336_280.html" frameborder="0" width="336" scrolling="no" height="280" leftmargin="0" topmargin="0">
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值