Is [1] in Muenchian grouping necessary?

Answering a large number of XSLT questions here in Stack Overflow, I am more than familiar with Muenchian grouping technology for grouping nodes during XSL conversion.

The expression used in it usually looks something like this:

*[generate-id() = generate-id(key('kSomeKey', .)[1])] 

It almost always contains that [1] , but it always struck me as odd.

The XSLT 1.0 specification defines generate-id() as follows:

The generate-id function returns a string that uniquely identifies the node in the node -set argument, which is first in document order .

(in italics)

It clearly states that the function works with the first node in document order, and in this context [1] will select the first node in the set in document order, so it seems that [1] is redundant.

This [1] used so widely that I hesitate to skip it, but it seems to be an outsider. Can anyone clarify this for me?

+5
source share
2 answers

Semantically [1] not required, but depending on (lack of) optimization in the XSLT processor, its use may be more efficient. This will depend on the internal components of each XSLT processor, whether key('key-name', foo)[1] only calculate one node or will it first compute the node set with all the nodes selected by the key, and then takes the first, how much it depends The XSLT processor will recognize generate-id(key('key-name', foo)) as an expression where only the first node in the node-set computed by the key is required.

+2
source

I would recommend always using the explicit "[1]" rather than using the fact that operations in XPath 1.0 do this implicitly. For two reasons: it improves the readability of your code and makes it compatible with XPath 2.0. There may be processors where this gives a performance advantage, but I would not speculate on this until it has been proven by measurement.

+4
source

Source: https://habr.com/ru/post/1209511/


All Articles