【问题标题】:Variables in XPATH expression in XSLT1XSLT1 中 XPATH 表达式中的变量
【发布时间】:2013-11-23 16:27:52
【问题描述】:

我以前为此使用 XSLT 2.0,但出于某些原因决定使其与 XSLT 1.0 兼容,并且根据我的阅读/发现,变量在 XSLT 1.0 的 XPATH 表达式中不起作用。

这是我在 XSLT 2.0 中使用的原始 XSL:

<xsl:template match="footnote_ref">
  <xsl:variable as="xs:integer" name="ref" select="."/>
  <xsl:variable name="fid" select="generate-id(../following-sibling::footnote_list[1]/footnote[$ref])"/>
  <sup><a href="#{$fid}">
    <xsl:value-of select="."/>
  </a></sup>
</xsl:template>

对于引用n,它将链接到下一个脚注部分中的nth 脚注。

在 XSLT 1.0 中这不起作用,它似乎忽略了 [$ref] 部分并且无论如何都只是链接到第一个脚注。我认为来自 EXSLT 的 dyn:evaluate 会起作用,所以我尝试了:

<xsl:variable name="fid" select="dyn:evaluate('generate-id(../following-sibling::footnote_list[1]/footnote[$ref])')"/>

如果我理解正确,这应该评估为generate-id(../following-sibling::footnote_list[1]/footnote[$ref]),但替换为$ref。在example 中,他们显示使用变量很好,但这仍然不起作用,并且表现得就像没有dyn:evaluate 一样,完全忽略了变量。

我是不是做错了什么或者误解了dyn:evaluate?如果重要的话,我通过 lxml (Python) 使用 libxslt。

【问题讨论】:

    标签: xml xslt xpath


    【解决方案1】:

    在 XSLT 1.0 中这不起作用,它似乎忽略了 [$ref] 部分,并且无论如何都只是链接到第一个脚注。

    XSLT 1.0 不支持 xsl:variable 上的 as 属性。所以在下面:

    <xsl:template match="footnote_ref">
      <xsl:variable name="ref" select="."/>
      <xsl:variable name="fid" select="generate-id(../following-sibling::footnote_list[1]/footnote[$ref])"/>
      <sup><a href="#{$fid}">
        <xsl:value-of select="."/>
      </a></sup>
    </xsl:template>
    

    $ref 是一个节点集,因此footnote[$ref] 中的最终谓词表示“选择所有footnote 元素,使得节点集$ref 不为空”(即所有这些元素)。当您将一组节点传递给generate-id 时,您会按文档顺序返回集合中的 first 节点的 ID。但是如果你说

      <xsl:variable name="ref" select="number(.)"/>
    

    那么$ref 是一个数字,谓词将执行您需要的基于position() 的测试,您应该得到正确的脚注。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-03
      • 2012-04-09
      • 2013-07-10
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 2020-05-29
      相关资源
      最近更新 更多