I\'m a beginner in XSLT and figured out that I cannot just add up numbers to a variable and change its value in any way.
I have a XML document with a list of numbers
I. Here is a simple, forward-only solution -- do note that no reverse axis is used and the time complexity is just O(N) and the space complexity is just O(1).
This is probably the simplest and fastest of all presented solutions:
No monstrous complexity or grouping is required at all ...
No variables, no keys (and no space taken for caching key->values), no sum() ...
This is an example of a streaming transformation -- it doesn't require the complete XML document tree to be present in memory and can be used to process documents of indefinite or infinite length.
When the transformation is applied on the provided source XML document:
the wanted, correct result is produced:
189.5
1.5
#191#
9.5
11
10
#30.5#
II. Update
The transformation above when run on sufficiently-big XML documents and with XSLT processors that don't optimize tail-recursion, causes stack overflow, due to a long chain of
Below is another transformation, which doesn't cause stack overflow even with extremely big XML documents. Again, no reverse axes, no keys, no "grouping", no conditional instructions, no count()
, no
...
And, most importantly, compared with the "efficient", key-based Muenchian grouping, this transformation takes only 61% of the time of the latter, when run on an XML document having 105 000 (105 thousand) lines:
Additionally, this transformation can be speeded to take less than 50% (that is, make it more than twice as fast) of the time taken by the Muenchian grouping transformation, by replacing every element name by just *
A lesson for us all to learn: A non-key solution sometimes can be more efficient than a key-based one.