获取XML数据并使用样式表格式化

        使用样式表格式化获得的XML数据,这个如果使用单独一个XML文件是完全可以的,在XML文件里面嵌入SXL文件,但是现在不要这么作,直接获取XML数据,然后调用XSL文件格式化数据。
        这部分演示程序需要使用到AJAX客户端框架,如下例子所示:
1.ceshi.jsp

None.gif <% @ page contentType = " text/html;charset=GBK " %>
None.gif
< script language = " javascript "  src = " js\request.js " ></ script >
None.gif
< script language = " javascript " >
None.gif
ExpandedBlockStart.gifContractedBlock.giffunction showXML()
dot.gif {
InBlock.gif    var url 
= "Vehicles.xml";
InBlock.gif    get_request( url, 
"text" );    
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.giffunction pageChange( responseText )
dot.gif
InBlock.gif        var xml 
= new ActiveXObject( "Microsoft.XMLDOM" );
InBlock.gif        xml.async 
= false;
InBlock.gif        
//load the text of xml data
InBlock.gif
        xml.loadXML( responseText );
InBlock.gif        
InBlock.gif        var xsl 
= new ActiveXObject( "Microsoft.XMLDOM" );
InBlock.gif        xsl.async 
= false;
InBlock.gif        
// Load the XSL
InBlock.gif
        xsl.load( "Vehicles.xsl" );
InBlock.gif        var div 
= document.getElementById( "show" );
InBlock.gif        div.innerHTML 
= xml.transformNode( xsl );
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*
InBlock.gif            load()函数加载XML文件
InBlock.gif            loadXML()函数加载字符串形式的XML数据
ExpandedSubBlockEnd.gif        
*/

ExpandedBlockEnd.gif}

None.gif
</ script >
None.gif
< p >
None.gif  
< input type = " submit "  name = " Submit "  value = " 提交 "  onclick = " showXML() "   />
None.gif
</ p >
None.gif
< div id = " show " ></ div >

 

2.Vehicles.xml

None.gif <? xml version="1.0" encoding="gb2312" ?>
None.gif
<!-- ?xml-stylesheet href='Vehicles.xsl' type='text/xsl'? -->
None.gif
< vehicles >
None.gif   
< vehicle  year ="1002"  make ="Land Rover"  model ="Discovery" >
None.gif         
< mileage > 36500 </ mileage >
None.gif         
< color > black </ color >
None.gif         
< price > $32999 </ price >
None.gif   
</ vehicle >
None.gif
</ vehicles >

3.Vehicles.xsl

None.gif <? xml version="1.0" ?>
None.gif
< xsl:stylesheet  xmlns:xsl ="http://www.w3.org/TR/WD=xsl" >
None.gif  
< xsl:template  match ="/" >
None.gif     
< html >
None.gif       
< head >
None.gif          
< title > uuu </ title >
None.gif       
</ head >
None.gif       
< body >
None.gif          
< table  align ="center"  border ="2" >
None.gif          
< tr >
None.gif            
< th > Year </ th >
None.gif            
< th > Make </ th >
None.gif            
< th > Model </ th >
None.gif            
< th > Mileage </ th >
None.gif            
< th > Color </ th >
None.gif            
< th > Price </ th >
None.gif          
</ tr >
None.gif          
< xsl:for-each  order-by ="+price"  select ="vehicles/vehicle" >
None.gif            
< tr >
None.gif              
< td >< xsl:value-of  select ="@year" /></ td >
None.gif              
< td >< xsl:value-of  select ="@make" /></ td >
None.gif              
< td >< xsl:value-of  select ="@model" /></ td >
None.gif              
< td >< xsl:value-of  select ="mileage" /></ td >
None.gif              
< td >< xsl:value-of  select ="color" /></ td >
None.gif              
< td >< xsl:value-of  select ="price" /></ td >
None.gif            
</ tr >
None.gif          
</ xsl:for-each >
None.gif          
</ table >
None.gif       
</ body >
None.gif     
</ html >
None.gif  
</ xsl:template >
None.gif
</ xsl:stylesheet >

4.说明
        需要注意的XML加载后,XSL文件的加载,以及 xml.transformNode( xsl ) 方法。

转载于:https://www.cnblogs.com/yongboy/archive/2006/03/22/356033.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值