开发者

unique elements with XSL generate-key()

开发者 https://www.devze.com 2023-02-17 02:06 出处:网络
First of all thank you to everyone who replied in my previous thread. In the interest of avoiding confusion I am posting similar information here, but questions updated accordingly.

First of all thank you to everyone who replied in my previous thread. In the interest of avoiding confusion I am posting similar information here, but questions updated accordingly.

My problem is that my xsl for-each shown below never gets executed, indicating that there is nothing in the result set. However, I cannot figure out why. Further description below.

Input structure

<AllMyResults>
<Result>
<someElement>value</state>
<otherElement>value 2</state>
<subject>Get unique subjects!</state>
</Result>
</AllMyResults>

At the top of my XSL file I have the key statement

<xsl:key name="SubjectKey" match="All_Results/Result" use="subject"/>

[2] The meat of my XSL file, which uses a different input structure:

<xsl:for-each select="$ResultSet/subject[
                         generate-id()
                          = generate-id(key('SubjectKey', 'subject')[1])
                      ]">
... this point is never reached ...
</xsl:for-each>

Input structure used by [2] above The input structure is just a list of elements.

What am I missing here? I used a debugger to determine that the for-each was never executed, which indicates that the set generated by the expression $ResultSet/subject[generate-id() = generate-id(key('SubjectKey', 'subject')[1])] was the empty set. But why?

Additional Info

$Res开发者_高级运维ultSet is a node set. It was a parameter passed in to the template. According to my debugger, the "key" statement gets executed the appropriate amount of times -- once per time a "subject" shows up in my input file. According to what I've read about generate-id(), with no params, it operates on the current node. Instead of $ResultSet/subject I've also tried all sorts of variations. ($ResultSet/*/subject, $ResultSet/*, etc)


I believe that should be key('SubjectKey', subject), not key('SubjectKey', 'subject') (note the quotes) and that the predicate should be on $ResultSet, not $ResultSet/subject:

$ResultSet[generate-id() = generate-id(key('SubjectKey', subject)[1])]


Have you tried as the key only needs to know what node it is matching on not the xpath, which seems to be what you are doing.

0

精彩评论

暂无评论...
验证码 换一张
取 消