我正在使用 xpath2 的 index-of 值返回排序后的节点序列中的 current() 的索引。使用 SAXON,排序后的节点序列是唯一的,但 index-of 返回两个值的序列。
这不会一直发生,只是偶尔发生,但不是出于任何我能找到的原因。有人可以解释发生了什么吗?
我已经根据例程给出这种奇怪行为的数据示例编写了一个最小示例。
源数据为:
<data>
<student userID="1" userName="user1"/>
<session startedOn="01/16/2012 15:01:18">
</session>
<session startedOn="11/16/2011 13:31:33">
</session>
</data>
我的 xsl 文档将会话节点放入根模板最顶部的排序序列 $orderd 中:
<xsl:template match="/">
<xsl:variable name="nodes" as="node()*" select="/data/session"></xsl:variable>
<xsl:variable name="orderd" as="node()*">
<xsl:for-each select="$nodes">
<xsl:sort select="xs:dateTime(xs:dateTime(concat(substring(normalize-space(@startedOn),7,4),'-',substring(normalize-space(@startedOn),1,2),'-',substring(normalize-space(@startedOn),4,2),'T',substring(normalize-space(@startedOn),12,8)))
)" order="ascending"/>
<xsl:sequence select="."/>
</xsl:for-each>
</xsl:variable>
由于节点已经由@startOn 排序,但顺序相反,序列 $orderd 应该与文档排序序列 $nodes 相同,但顺序相反。
当我使用 for-each 语句创建输出时,我发现在使用 index-of 进行测试时,不知何故这两个节点被视为相同。
下面的代码用于输出数据(紧跟在上面的块之后):
<output>
<xsl:for-each select="$nodes">
<xsl:sort select="position()" order="descending"></xsl:sort>
<xsl:variable name="index" select="index-of($orderd,current())" as="xs:integer*"></xsl:variable>
<xsl:variable name="pos" select="position()"></xsl:variable>
<session reverse-documentOrder="{$pos}" sortedOrder="{$index}"/>
</xsl:for-each>
</output>
正如输出(如下所示)所示,index-of 函数返回序列 (1,2),这意味着它将两个节点视为相同。我检查了用于对值进行排序的表达式,它产生了不同且格式正确的日期时间字符串。
<output>
<session reverse=documentOrder="1"
sortedOrder="1 2"/>
<session reverse-documentOrder="2"
sortedOrder="1 2"/>
</output>