0

I experienced a strange behaviour of the saxon XSLT processor which I don't know whether it's a bug or a feature: generate-id() does not generate the same ID on identical nodes. (To make things clear, I use a very short transformation stylesheet. One can achieve the same output in a simpler way, but then we don't see the effect.)

The input XML is as simple as follows:

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

This input is transformed by

<?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>

Using this stylesheet all IDs generated in mkIndex start with d2 while the "basic" IDs start with d1.

If you use the variable $rootParam instead of $rootVar the IDs generated in mkIndex look the same as the "basic" IDs. Texts are the same in both cases, therefore we handle the same nodes in both cases.

I don't understand why identical nodes get different IDs with generate-id(). Maybe someone can explain the difference.

Thank you,

leu

4

1 回答 1

3

代码很好

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

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

所以将该代码更改为

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

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

于 2013-09-02T16:36:49.070 回答