I am given this XML and have to render quite a bit from it and most is working fine, but I am stomped trying to extract the node-set of color
whos key matches the key
of the bar
element and the attribute is a hardcoded string ('data'
in this case). The node-set is to be passed as parameter to a template and each color line must only appear once:
<report>
<settings>
<colors>
<color key="1-1" name="frame" value="..." ... />
<color key="1-1" name="data" value="..." ... />
<color key="2-1" name="frame" value="..." ... />
<color key="2-1" name="data" value="..." ... />
<color key="3-1" name="frame" value="..." ... />
<color key="3-1" name="data" value="..." ... />
</colors>
<comp>
<cont>
<bar key="1-1" .../>
<bar key="1-1" .../>
<bar key="2-1" .../>
</cont>
<comp>
<!-- possibly more <comp/cont/bar> below that may not be mixed with the above -->
</settings>
</report>
In my XSLT file I have this (extract):
<xsl:key name="barnode" match="bar" use="@key"/>
<xsl:key name="colorlookup" match="/report/settings/colors/color" use="@key"/>
<!-- this runs at the `cont` element level, i.e. `bar` can be accessed without prefix -->
<!-- set $x to the node-list of bars with unique @key attribute -->
<xsl:call-template name="renderit">
<xsl:with-param name="colors">
<!-- 'bars' contains node-set of 'bar' elements with @key being unique -->
<xsl:variable name="bars" select="bar[generate-id() = generate-id(key('barnode', @key)[1])]"/>
<xsl:for-each select="$bars">
<xsl:value-of select="key('colorlookup', @key)[@name='data']"/>
</xsl:for-each>
</xsl:with-param>
</xsl:call-template>
The problem is, that this does not pass a node-set, but a tree-fragment. Is it possible to make a select that does the same as above, but returns a node-set?
Edit:
Expected node-set:
<color key="1-1" name="data" value="..." ... />
<color key="2-1" name="data" value="..." ... />
I am not sure if the presented XSLT will even generate this result tree fragment as I don't know how to print it (for debug purposes).
Try
<xsl:with-param name="colors" select="key('colorlookup', bar[generate-id() = generate-id(key('barnode', @key)[1])]/@key)[@name = 'data']"/>