2

A有以下xml:

data_0.xml data_1.xml data_3.xml 等等...

在 xslt 文件中我想遍历所有文件,所以我尝试了 for-each 函数。

<xsl:for-each select="document('data.xml')/*">

如何迭代所有这些?以某种方式添加面具?这肯定行不通:

<xsl:for-each select="document('data_*.xml')/*">
4

1 回答 1

0

这是您在 xslt 1.0 中的解决方案:

我的文件系统中有四个文件:

文档1.xml:

<p>Doc1</p>

文档2.xml:

<p>Doc2</p>

Doc3.xml:

<p>Doc3</p>

Doc4.xml:

<p>Doc4</p>

我的xslt得到他们的输出是:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


  <xsl:template match="/">
    <Root>
      <xsl:call-template name="getDocuments"/>
    </Root>
  </xsl:template>

<xsl:template name="getDocuments">
  <xsl:param name="fileStartWith" select="'Doc'"/>
  <xsl:param name="endCounter">4</xsl:param>
  <xsl:param name="startCounter">1</xsl:param>
  <xsl:choose>
    <xsl:when test="$endCounter > 0">
      <xsl:variable name="fileName"><xsl:value-of select="concat($fileStartWith,$startCounter,'.xml')"/></xsl:variable>
      <xsl:for-each select="document($fileName)/*">
        <xsl:copy-of select="."/><xsl:text>&#10;</xsl:text>
      </xsl:for-each>
      <xsl:call-template name="getDocuments">
        <xsl:with-param name="startCounter" select="$startCounter + 1"/>
        <xsl:with-param name="fileStartWith" select="$fileStartWith"/>
        <xsl:with-param name="endCounter" select="$endCounter - 1"/>
      </xsl:call-template>
    </xsl:when>
  </xsl:choose>

</xsl:template>

</xsl:stylesheet>

生成的输出是:

<Root>
<p>Doc1</p>
<p>Doc2</p>
<p>Doc3</p>
<p>Doc4</p>
</Root>

请确保 xslt 和 xml 在同一路径上,否则您需要更改文档功能的内容。

于 2013-09-09T05:52:21.813 回答