XSLT转换XML小结

XML file:

<? 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 >

    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 >

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.xslt " ));
        transform.Transform(Server.MapPath(
" a.xml " ),  null , writer);
        writer.Close();
        Response.Clear();
        Response.AddHeader(
" Content-Disposition " " attachment; filename= "   +  fileName);
        Response.WriteFile(fullPath);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值