I’m using the Muenchian method to group nodes in an XML document.
As part of my output, I’d like to select all of the nodes to which I have not assigned a key.
I’ve tried
<xsl:apply-templates select="*[key('kcWWPN','')]"/>
but that doesn’t appear to be working correctly, in that it is selecting no nodes.
Any suggestions on the right way to do this?
Good question, +1.
Here are two different, but simple solutions:
For simplicity I will assume that we are keying only elements, but the two solutions below can naturally be extended to cover other types of nodes.
I. Simply, define a counter-key:
when this transformation is applied on the following XML document (borrowed from @lwburk):
the wanted, correct result is produced:
II. Use the set difference of all elements and all keyed elements (the latter is found using Muenchian grouping):
This solution is simpler than the first, because one doesn’t need to compose a counter-key:
when this transformation is applied on the same XML document (above), again the same, correct result is produced:
Note: The last solution may not work when the key is not a node, but result of a function, such as
substring-before(). In this case we simply modify the original key, so thet itsuseattribute is simply:use="."and use this solution with the modified original key. It can be prooven that this procedure produces the correct, wanted set of elements.