xslt操作xml文件实例
现在xml运用的很广,下面我通过一个实例来展示用xslt来操作xml文件。(生成xml文件请看我以前的日志)
时间比较匆忙,本文有点简陋;我会不定期更新和完善这篇文章。
a.xml
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>
<?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
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>
<?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
Code
<asp:Xml ID="ResourcesList" runat="server" DocumentSource="a.xml" TransformSource="a.xsl"></asp:Xml>
<asp:Xml ID="ResourcesList" runat="server" DocumentSource="a.xml" TransformSource="a.xsl"></asp:Xml>
a.aspx.cs
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;
}
}
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;
}
}