在XSLT中,为什么我不能使用xsl:attribute设置value-of的select属性?有什么好的替代方法吗?

3
我有一个常量和一个变量,我想将它们合并在一起以选择特定的节点,这就是我想做的事情:
<xsl:attribute name="value">
 <xsl:value-of>
  <xsl:attribute name="select">
   <xsl:text>/root/meta/url_params/
   <xsl:value-of select="$inputid" />
  </xsl:attribute>
 </xsl:value-of>
</xsl:attribute>

为什么它不能工作,我应该怎么做呢?

1
你的输入是什么?你想选择什么?变量 $inputid 会是什么类型的值? - user357812
肯定会有解决问题的方法。您能否解释一下您实际上想要转换什么(简短的示例文档和期望的输出)? - Dirk Vollmar
好问题 (+1)。当 $inputid 的值只是元素名称时,请查看我的答案以获取解决方案。 - Dimitre Novatchev
@Kristoffer-Nolgren,@Alejandro,@0xA3和@Max-Toro:我已经更新了我的答案,使用动态评估更一般类型的XPath表达式。 - Dimitre Novatchev
2个回答

7

虽然 @Alejandro 是正确的,通常情况下需要动态评估(这可能在 XSLT 2.1+ 中提供),但还有一些可管理的简单情况。

例如,如果 $inputid 只包含一个名称,您可能想要这样做

<xsl:value-of select="/root/meta/url_params/*[name()=$inputid]"/>

我们可以实现一个相当通用的动态XPath评估器,只要我们将每个位置路径限制为元素名称即可。
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:param name="inputId" select="'param/yyy/value'"/>

 <xsl:variable name="vXpathExpression"
  select="concat('root/meta/url_params/', $inputId)"/>

 <xsl:template match="/">
  <xsl:value-of select="$vXpathExpression"/>: <xsl:text/>

  <xsl:call-template name="getNodeValue">
    <xsl:with-param name="pExpression"
         select="$vXpathExpression"/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template name="getNodeValue">
   <xsl:param name="pExpression"/>
   <xsl:param name="pCurrentNode" select="."/>

   <xsl:choose>
    <xsl:when test="not(contains($pExpression, '/'))">
      <xsl:value-of select="$pCurrentNode/*[name()=$pExpression]"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="getNodeValue">
        <xsl:with-param name="pExpression"
          select="substring-after($pExpression, '/')"/>
        <xsl:with-param name="pCurrentNode" select=
        "$pCurrentNode/*[name()=substring-before($pExpression, '/')]"/>
      </xsl:call-template>
    </xsl:otherwise>
   </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

当该转换应用于此XML文档时:

<root>
  <meta>
    <url_params>
      <param>
        <xxx>
          <value>5</value>
        </xxx>
      </param>
      <param>
        <yyy>
          <value>8</value>
        </yyy>
      </param>
    </url_params>
  </meta>
</root>
期望的正确结果已经生成:
root/meta/url_params/param/yyy/value: 8

1
应该是 name()=$inputid,不包括引号。 - Max Toro
太棒了!"name()=$inputid"会被称为什么,如果我想要了解更多关于这个语法的知识,这种语法有一个名称吗? - Himmators
@Kristoffer-Nolgren:方括号[...]中的任何内容都被称为“谓词”。在这里定义:http://www.w3.org/TR/xpath/#predicates。谓词可以包含任何XPath表达式;它会被计算,如果结果不是布尔类型,则转换为布尔值。 - Dimitre Novatchev

5

XSLT 1.0标准中XPath表达式没有运行时评估。

因此,根据$inputid的不同,您可能有不同的解决方案。

但是/root/meta/url_params/$inputid是错误的,因为在XPath 1.0中,/右侧必须是相对路径(在XPath 2.0中也可以是函数调用)。

对于这个特定情况,您可以使用:

/root/meta/url_params/*[name()=$inputid]

或者

/root/meta/url_params/*[@id=$inputid]

对于一般情况,我会选择像Dimitre答案中所提到的walker模式。

我该如何解决这个问题?$inputid 在这种情况下等于类似于“汽车”或“乘客”等。 - Himmators

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接