Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the [1] in Muenchian grouping really necessary?

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?

like image 482
JLRishe Avatar asked Dec 19 '14 05:12

JLRishe


1 Answers

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.

like image 174
Michael Kay Avatar answered Sep 20 '22 07:09

Michael Kay