0
votes

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

$ResultSet 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)

2
You are matching Result elements in key declaration... - user357812
Your XML has an element called AllMyResults but your key definition has All_Results. Almost certainly a typo, but if you make typos in your question, it's hard to trust any of the information you give us. - Michael Kay
You close the <someElement> element with </state> - to me, this does not seem like valid XML. - Ruprecht von Waldenfels

2 Answers

0
votes

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])]
0
votes

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.