Having answered a large number of XSLT questions here on Stack Overflow, I am more than familiar with the Muenchian grouping technique for grouping nodes during an XSL transformation.
The expression used therein is usually something like this:
*[generate-id() =
generate-id(key('kSomeKey', .)[1])]
It almost invariably contains that [1]
, but this has always struck me as odd.
The XSLT 1.0 spec defines generate-id()
as follows:
The generate-id function returns a string that uniquely identifies the node in the argument node-set that is first in document order.
(emphasis added)
It clearly states that the function operates on the first node in document order, and in this context, the [1]
would be selecting the first node in the set in document order, so it seems that the [1]
is redundant.
This [1]
is used so widely that I am hesitant to omit it, but it seems extraneous. Can anyone clear this up for me?
I would recommend always using an explicit "[1]" rather than exploiting the fact that operations in XPath 1.0 do it implicitly. For two reasons: it improves the readability of your code, and it makes it compatible with XPath 2.0. There may be processors where it gives a performance benefit, but I wouldn't speculate on that until it's been proved by measurement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With