是的,这是绝对可能的。
它的简单程度取决于文件名和文件夹名模式的可靠性。你可以使用
document() function
检查结果文件是否存在。使用递归继续查找下一个按顺序命名的文件,直到到达末尾。
从示例文件名中,可以使用以下XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<summary>
<xsl:call-template name="getResults">
<!--First call, use empty index -->
<xsl:with-param name="index" />
</xsl:call-template>
</summary>
</xsl:template>
<xsl:template name="getResults">
<xsl:param name="index" />
<xsl:variable name="filename">
<xsl:choose>
<xsl:when test="$index =''">
<xsl:value-of select="concat('result', '.xml')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('result', $index, '.xml')" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:choose>
<!--Check for the presence of the file -->
<xsl:when test="document($filename)">
<!--If it exists, then output something from the document-->
<Result>
<xsl:attribute name="href">
<xsl:value-of select="$filename" />
</xsl:attribute>
<xsl:copy-of select="document($filename)/Succeeded" />
</Result>
<!--Then, call the function with a new index value to look for the next file-->
<xsl:call-template name="getResults">
<xsl:with-param name="index">
<xsl:choose>
<!--If empty index, start at 1 -->
<xsl:when test="$index=''">
<xsl:value-of select="1" />
</xsl:when>
<xsl:otherwise>
<!--Add 1 to the previous index value -->
<xsl:value-of select="number($index+1)" />
</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:comment>Done looking for Results. Found <xsl:value-of select="number($index)" /> files.</xsl:comment>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
我在与xslt相同的文件夹(即result.xml、result1.xml、result2.xml)中运行了3个结果文档,并生成了以下输出:
<?xml version="1.0" encoding="utf-8"?>
<summary>
<Result href="result.xml">
<Succeeded>
<someStrongXMLObject>
</someStrongXMLObject>
</Succeeded>
</Result>
<Result href="result1.xml">
<Succeeded>
<someStrongXMLObject>
</someStrongXMLObject>
</Succeeded>
</Result>
<Result href="result2.xml">
<Succeeded>
<someStrongXMLObject>
</someStrongXMLObject>
</Succeeded>
</Result>
<!--Done looking for Results. Found 3 files.-->
</summary>