xslt操作xml文件实例

现在xml运用的很广,下面我通过一个实例来展示用xslt来操作xml文件。(生成xml文件请看我以前的日志)

时间比较匆忙,本文有点简陋;我会不定期更新和完善这篇文章。

a.xml

 

ContractedBlock.gif ExpandedBlockStart.gif Code
<?xml version="1.0" encoding="utf-8"?>
<resources>
  
<category name="All" label="所有资源">
      
<category name="category1" label="分类1"  thumbnail="a.jpg">
        
<resource name="分类1图片1" path="A1.jpg" thumbnail="a1.jpg" />
        
<resource name="分类1图片2" path="A2.jpg" thumbnail="a2.jpg" />
    
</category>
    
<category name="category2" label="分类2"  thumbnail="b.jpg">
      
<category name="category2-1" label="分类2-1"  thumbnail="b.jpg">
        
<resource name="分类2-1-1" path="B1.jpg" thumbnail="b1.jpg" />
        
<resource name="分类2-1-2" path="B2.jpg" thumbnail="b2.jpg" />
      
</category>
      
<category name="category2-2" label="分类2-2"  thumbnail="c.jpg">
        
<resource name="分类2-2-1" path="C1.jpg" thumbnail="c1.jpg" />
        
<resource name="分类2-2-2" path="C2.jpg" thumbnail="c2.jpg" />
      
</category>
      
<category name="category2-3" label="分类2-3"  thumbnail="d.jpg">
        
<resource name="分类2-3-1" path="D1.jpg" thumbnail="d1.jpg" />
        
<resource name="分类2-3-2" path="D2.jpg" thumbnail="d2.jpg" />
      
</category>
    
</category>
    
<category name="category3" label="分类3"  thumbnail="e.jpg">
        
<resource name="分类1图片1" path="E1.jpg" thumbnail="e1.jpg" />
        
<resource name="分类1图片2" path="E2.jpg" thumbnail="e2.jpg" />
    
</category>  
  
</category>
</resources>

 

a.xsl

 

ContractedBlock.gif ExpandedBlockStart.gif Code
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
<xsl:param name="currentPath"/>
  
<xsl:template match="/">
    
<xsl:for-each select="category">
      
<div style="font-size: 10pt;padding:2px;">
        
<div style="color:#FFFFFF;+line-height:21px;background:-color:#069;font-size:13px;font-weight:bold;line-height:18px;width:100px;height:18px;overflow:hidden;float:left">
          
<xsl:value-of select="@label"/>
        
</div>
      
</div>
      
<div style="width:100%; height:auto;">
        
<ul style="list-style:none">
          
<xsl:for-each select="./category">
            
<li style="float:left; padding:10px; width: 120px; overflow:hidden; text-align:center;">
              
<a href="DownloadCenter.aspx?id={concat(concat($currentPath, '/'), @name)}">
                
<img  src="{@thumbnail}" height="90px" alt="{@label}"/>
              
</a>
              
<h5>
                
<a href="a.aspx?id={concat(concat($currentPath, '/'), @name)}">
                  
<xsl:value-of select="@label"/>
                
</a>
              
</h5>
            
</li>
          
</xsl:for-each>
        
</ul>
      
</div>
      
<div style="width:100%; height:auto;">
        
<ul style="list-style:none">
          
<xsl:for-each select="./resource">
            
<li style="float:left; padding:10px; width: 120px; overflow:hidden; text-align:center;">
              
<a href="{@path}">
                
<img  src="{@thumbnail}" height="90px" alt="{@name}"/>
              
</a>
              
<h5>
                
<a href="{@path}">
                  
<xsl:value-of select="@name"/>
                
</a>
              
</h5>
            
</li>
          
</xsl:for-each>
        
</ul>
      
</div>
      
<hr style="clear:both"/>
    
</xsl:for-each>
  
</xsl:template>

</xsl:stylesheet>

 

 

a.aspx

 

ContractedBlock.gif ExpandedBlockStart.gif Code
<asp:Xml ID="ResourcesList" runat="server" DocumentSource="a.xml" TransformSource="a.xsl"></asp:Xml>

 

a.aspx.cs

 

ContractedBlock.gif ExpandedBlockStart.gif cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Xml;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class xslt_xml_show : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
string path = Request.QueryString["id"];
        
string xpath = "/resources/category";
        
if (!String.IsNullOrEmpty(path))
        {
            
foreach (string segment in path.Split('/'))
            {
                
if (String.IsNullOrEmpty(segment))
                {
                    
continue;
                }
                xpath 
+= "/category[@name='" + segment + "']";
            }
        }
        
else
        {
            path 
= "";
        }
        resources.TransformArgumentList 
= new System.Xml.Xsl.XsltArgumentList();
        XmlDocument dom 
= new XmlDocument();
        dom.Load(Server.MapPath(
"~/xslt-xml/source.xml"));
        XmlNode node 
= dom.SelectSingleNode(xpath);
        resources.DocumentContent 
= node.OuterXml;
        resources.TransformArgumentList.AddParam(
"currentPath""", path);
    }

    
private bool IsLastInPath(string path, string target)
    {
        
string[] pathsegments = path.Split('/');
        
for (int i = pathsegments.Length - 1; i >= 0; i--)
        {
            
string segment = pathsegments[i];
            
if (String.IsNullOrEmpty(segment))
            {
                
continue;
            }
            
if (segment == target)
            {
                
return true;
            }
            
else
            {
                
return false;
            }
        }
        
return false;
    }
}

 

转载于:https://www.cnblogs.com/david-lee/archive/2009/03/11/1408492.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值