你计划如何实现你的树将决定你需要如何构建你的输出。
这是一个使用jQuery treeView的示例。
XML 输入
<Root>
<folder name="aaa">
<folder name="abb">
<file name="ccc.pdf" url="c:\test\ccc.pdf"/>
<file name="ddd.pdf" url="c:\test\ddd.pdf"/>
<file name="eee.pdf" url="c:\test\eee.pdf"/>
<file name="fff.pdf" url="c:\test\fff.pdf"/>
</folder>
<folder name="acc">
<file name="ggg.pdf" url="c:\test\ggg.pdf"/>
<file name="hhh.pdf" url="c:\test\hhh.pdf"/>
</folder>
</folder>
<folder name="bbb">
<folder name="bba">
<file name="iii.pdf" url="c:\test\iii.pdf"/>
</folder>
<folder name="bbc" />
</folder>
<folder name="ccc">
<file name="iii.pdf" url="c:\test\iii.pdf"/>
<file name="jjj.pdf" url="c:\test\jjj.pdf"/>
<file name="kkk.pdf" url="c:\test\kkk.pdf"/>
<file name="lll.pdf" url="c:\test\lll.pdf"/>
</folder>
</Root>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
<title>StackOverflow Test of jQuery treeView</title>
<link rel="stylesheet" href="../jquery.treeview.css" />
<link rel="stylesheet" href="../red-treeview.css" />
<link rel="stylesheet" href="screen.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script src="../lib/jquery.cookie.js" type="text/javascript"></script>
<script src="../jquery.treeview.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$("#browser").treeview();
});
</script>
</head>
<body>
<ul id="browser" class="filetree">
<xsl:apply-templates/>
</ul>
</body>
</html>
</xsl:template>
<xsl:template match="folder">
<li>
<span class="folder"><xsl:value-of select="@name"/></span>
<xsl:if test="folder">
<ul>
<xsl:apply-templates select="folder"/>
</ul>
</xsl:if>
<xsl:if test="file">
<ul>
<xsl:apply-templates select="file"/>
</ul>
</xsl:if>
</li>
</xsl:template>
<xsl:template match="file">
<li>
<span class="file">
<a href="file:///{translate(@url,'/','\')}">
<xsl:value-of select="@name"/>
</a>
</span>
</li>
</xsl:template>
</xsl:stylesheet>
HTML 输出
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>StackOverflow Test of jQuery treeView</title>
<link rel="stylesheet" href="../jquery.treeview.css">
<link rel="stylesheet" href="../red-treeview.css">
<link rel="stylesheet" href="screen.css"><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script><script src="../lib/jquery.cookie.js" type="text/javascript"></script><script src="../jquery.treeview.js" type="text/javascript"></script><script type="text/javascript">
$(function() {
$("#browser").treeview();
});
</script></head>
<body>
<ul id="browser" class="filetree">
<li><span class="folder">aaa</span><ul>
<li><span class="folder">abb</span><ul>
<li><span class="file"><a href="file:///c:\test\ccc.pdf">ccc.pdf</a></span></li>
<li><span class="file"><a href="file:///c:\test\ddd.pdf">ddd.pdf</a></span></li>
<li><span class="file"><a href="file:///c:\test\eee.pdf">eee.pdf</a></span></li>
<li><span class="file"><a href="file:///c:\test\fff.pdf">fff.pdf</a></span></li>
</ul>
</li>
<li><span class="folder">acc</span><ul>
<li><span class="file"><a href="file:///c:\test\ggg.pdf">ggg.pdf</a></span></li>
<li><span class="file"><a href="file:///c:\test\hhh.pdf">hhh.pdf</a></span></li>
</ul>
</li>
</ul>
</li>
<li><span class="folder">bbb</span><ul>
<li><span class="folder">bba</span><ul>
<li><span class="file"><a href="file:///c:\test\iii.pdf">iii.pdf</a></span></li>
</ul>
</li>
<li><span class="folder">bbc</span></li>
</ul>
</li>
<li><span class="folder">ccc</span><ul>
<li><span class="file"><a href="file:///c:\test\iii.pdf">iii.pdf</a></span></li>
<li><span class="file"><a href="file:///c:\test\jjj.pdf">jjj.pdf</a></span></li>
<li><span class="file"><a href="file:///c:\test\kkk.pdf">kkk.pdf</a></span></li>
<li><span class="file"><a href="file:///c:\test\lll.pdf">lll.pdf</a></span></li>
</ul>
</li>
</ul>
</body>
</html>
呈现的 HTML 输出