我有一个列出xml文件的目录,由http://code.google.com/p/xml-dir-listing/自动生成,它看起来像这样:
<?xml version="1.0" encoding="UTF-8" ?>
<directory name="rootdir" size="256" lastModified="1408535531000" date="20140820T145211" absolutePath="/path/to/rootdir" sort="name" reverse="true">
<directory name="20140819" size="256" lastModified="1408526538000" date="20140820T122218" absolutePath="/path/to/rootdir/20140819">
<directory name="file1" size="4096" lastModified="1408526538000" date="20140820T122218" absolutePath="/path/to/rootdir/20140819/file1" />
<directory name="file2" size="4096" lastModified="1408526538000" date="20140820T122218" absolutePath="/path/to/rootdir/20140819/file2" /></directory>
<directory name="20140818" size="256" lastModified="1408526552000" date="20140820T122232" absolutePath="/path/to/rootdir/20140818">
<directory name="file1" size="4096" lastModified="1408526552000" date="20140820T122232" absolutePath="/path/to/rootdir/20140818/file1" />
<directory name="file2" size="4096" lastModified="1408526552000" date="20140820T122232" absolutePath="/path/to/rootdir/20140818/file2" /></directory>
</directory>
我需要一个xslt转换才能获得这样的home.html文件:
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Heading</h1>
<ul>
<li><a href="/rootdir/20140819/file1/index.html">rootdir/20140819/file1</a>
</li>
<li><a href="/rootdir/20140819/file2/index.html">rootdir/20140819/file2</a>
</li>
<li><a href="/rootdir/20140818/file1/index.html">rootdir/20140818/file1</a>
</li>
<li><a href="/rootdir/20140818/file2/index.html">rootdir/20140818/file2</a>
</li>
</ul>
<body>
</html>
我需要它,所以每次运行转换时,我的列表都会用新目录更新。我相信它可能是非常容易的任务,但我对xslt完全不熟悉,即使我花了相当多的时间阅读它,但它对我来说仍然不直观。请帮忙。
修改
<?xml version="1.0" encoding="UTF-8"?>
<directory name="rootdir" size="256" lastModified="1408603833000" date="20140821T095033" absolutePath="/path/to/rootdir" sort="name" reverse="true">
<directory name="20140819" size="256" lastModified="1408526538000" date="20140820T122218" absolutePath="/path/to/rootdir/20140819">
<directory name="file1" size="4096" lastModified="1408526538000" date="20140820T122218" absolutePath="/path/to/rootdir/20140819/file1">
<directory name="test" size="4096" lastModified="1408526538000" date="20140820T122218" absolutePath="/path/to/rootdir/20140819/file1/test"/>
<file name="index.html" size="676" lastModified="1408526538000" date="20140820T122218" absolutePath="/path/to/rootdir/20140819/file1/index.html"/>
</directory>
<directory name="file2" size="4096" lastModified="1408526538000" date="20140820T122218" absolutePath="/path/to/rootdir/20140819/file2">
<directory name="test" size="256" lastModified="1408526538000" date="20140820T122218" absolutePath="/path/to/rootdir/20140819/file2/test"/>
<file name="index.html" size="676" lastModified="1408526538000" date="20140820T122218" absolutePath="/path/to/rootdir/20140819/file2/index.html"/>
</directory>
</directory>
<directory name="20140818" size="256" lastModified="1408526552000" date="20140820T122232" absolutePath="/path/to/rootdir/20140818">
<directory name="file1" size="4096" lastModified="1408526552000" date="20140820T122232" absolutePath="/path/to/rootdir/20140818/file1">
<directory name="test" size="256" lastModified="1408526552000" date="20140820T122232" absolutePath="/path/to/rootdir/20140818/file1/test"/>
<file name="index.html" size="676" lastModified="1408526552000" date="20140820T122232" absolutePath="/path/to/rootdir/20140818/file1/index.html"/>
</directory>
<directory name="file2" size="4096" lastModified="1408526552000" date="20140820T122232" absolutePath="/path/to/rootdir/20140818/file2">
<directory name="test" size="256" lastModified="1408526552000" date="20140820T122232" absolutePath="/path/to/rootdir/20140818/file2/test"/>
<file name="index.html" size="676" lastModified="1408526552000" date="20140820T122232" absolutePath="/path/to/rootdir/20140818/file2/index.html"/>
</directory>
</directory>
</directory>
这是包含index.html的xml输出
答案 0 :(得分:1)
首先要有一个与文档节点相匹配的模板,您可以在其中输出包含的html元素,如下所示:(为简洁起见我删除了一些html)
<xsl:template match="/">
<ul>
<xsl:apply-templates />
</ul>
</xsl:template>
您的主要问题是在输出文件信息时将目录名列表连接成一个长字符串。实现此目的的一种方法是通过参数将目录名从父级传递给子级。您将拥有一个匹配directory
的模板,然后将其连接到当前路径参数,并将其传递给匹配任何子元素的模板。
<xsl:template match="directory">
<xsl:param name="path" />
<xsl:apply-templates>
<xsl:with-param name="path" select="concat($path, '/', @name)" />
</xsl:apply-templates>
</xsl:template>
然后,您可以在匹配file
<xsl:template match="file">
<xsl:param name="path" />
<li>
<a href="{concat($path, '/', @name)}"><xsl:value-of select="$path" /></a>
</li>
</xsl:template>
试试这个XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="directory">
<xsl:param name="path" />
<xsl:apply-templates>
<xsl:with-param name="path" select="concat($path, '/', @name)" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="file">
<xsl:param name="path" />
<li>
<a href="{concat($path, '/', @name)}"><xsl:value-of select="$path" /></a>
</li>
</xsl:template>
<xsl:template match="/">
<html>
<body>
<ul>
<xsl:apply-templates />
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>