在XSLT中连接两个xPath

时间:2014-02-06 04:22:51

标签: xslt xpath concat

在我的XSLT脚本中,我正在调用一个递归模板,我希望将xPath作为参数传递给每次迭代。在每次迭代中,我想将子的xPath (*/)连接到当前路径(请参阅代码)。所以我使用concat()函数,因为它返回一个字符串,我无法使用该路径打印该路径的内容。

<xsl:copy-of select="$path" /> <!--This requests a xPath, not a string-->

所以任何人都可以告诉我如何连接两个xpath或如何将字符串转换为xpath。

谢谢。

    <xsl:template match="/">
        <xsl:call-template name="repeatable" >
            <xsl:with-param name="limit" select="10" />
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="repeatable">
        <xsl:param name="index" select="1" />
        <xsl:param name="limit" select="1" />
        <xsl:param name="path" select="@*" />

        <xsl:copy-of select="$path" />

        <xsl:if test="($limit >= $index)">
            <xsl:call-template name="repeatable">
                <xsl:with-param name="index" select="$index + 1" />
                <xsl:with-param name="path" select="concat('*/', $path)" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

2 个答案:

答案 0 :(得分:1)

虽然我等着您回复上面的问题,但这里有一个XSLT可以执行似乎尝试做的事情:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="yes" omit-xml-declaration="yes"/>

  <xsl:template match="@*">
    <xsl:value-of select="concat(name(), ' = ', ., '&#xA;')"/>
  </xsl:template>

  <xsl:template match="/">
    <xsl:call-template name="repeatable" >
      <xsl:with-param name="limit" select="10" />
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="repeatable">
    <xsl:param name="index" select="1" />
    <xsl:param name="limit" select="1" />
    <xsl:param name="current" select="." />

    <xsl:apply-templates select="$current/@*" />

    <xsl:if test="($limit >= $index)">
      <xsl:call-template name="repeatable">
        <xsl:with-param name="index" select="$index + 1" />
        <xsl:with-param name="limit" select="$limit" />
        <xsl:with-param name="current" select="$current/*" />
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

当在以下输入上运行时:

<root a1="a" a2="b">
  <cont a3="c" a4="d">
    <child a5="e" a6="f" />
  </cont>
  <cont a7="g" a8="h">
    <child a9="i" a10="j">
      <subchild a11="k" a12="l" />
    </child>
  </cont>
</root>

结果是:

a1 = a
a2 = b
a3 = c
a4 = d
a7 = g
a8 = h
a5 = e
a6 = f
a9 = i
a10 = j
a11 = k
a12 = l

这接近你想要做的吗?如果没有,请澄清。

答案 1 :(得分:0)

您要做的是动态创建xpath并使用它。这在XSLT3.0中是可能的,并且要使用的函数是evaluate()。

相关问题