generate-id在相同节点上生成不同的id

时间:2013-09-02 16:13:17

标签: xml xslt-2.0

我遇到了撒克逊XSLT处理器的奇怪行为,我不知道它是一个错误还是一个功能:generate-id()不会在相同的节点上生成相同的ID。 (为了清楚起见,我使用了一个非常短的转换样式表。可以用更简单的方式实现相同的输出,但是我们看不到效果。)

输入XML如下所示:

<list>
<entry>beta</entry>
<entry>gamma</entry>
<entry>alpha</entry>
</list>

此输入由

转换
<?xml version="1.0" encoding="utf-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:template match="/">
    <output>
      <xsl:apply-templates/>

      <xsl:call-template name="mkIndex">
        <xsl:with-param name="rootParam" select="." />
      </xsl:call-template>
    </output>
  </xsl:template>

  <xsl:template match="entry">
    <a id="{generate-id(.)}">
      <xsl:apply-templates/>
    </a>
  </xsl:template>

  <xsl:template name="mkIndex">
    <xsl:param name="rootParam"/>
    <xsl:variable name="rootVar">
      <xsl:copy-of select="."/>
    </xsl:variable>

    <!-- use $rootParam here to get the correct IDs with generate-id()-->
    <xsl:for-each select="$rootVar//entry">
      <xsl:sort select="."/>
      <a href="#{generate-id(.)}">
        <xsl:copy-of select="text()"/>
      </a>
    </xsl:for-each>
  </xsl:template>
</xsl:transform>

使用此样式表,mkIndex中生成的所有ID都以d2开头,而“基本”ID则以d1开头。

如果您使用变量$rootParam而不是$rootVar,则mkIndex中生成的ID与“基本”ID相同。两种情况下的文本都是相同的,因此我们在两种情况下都处理相同的节点。

我不明白为什么相同的节点使用generate-id()获得不同的ID。也许有人可以解释这种差异。

谢谢,

亮氨酸

1 个答案:

答案 0 :(得分:3)

代码

<xsl:variable name="rootVar">
  <xsl:copy-of select="."/>
</xsl:variable>

使用新节点创建一个临时树(这些节点是输入树中的节点的深层副本,但与它们不同)。

所以将代码更改为

<xsl:variable name="rootVar" select="."/>

如果您想使用输入树中的节点。