There is probably a very easy solution for this problem. I could easily do this in C#-LINQ. Unfortunately, I’m not so experienced with XPath and XSL.
I have an input XML file that contains the following structure:
<group>
<val>1</val>
<val>3</val>
<val>1</val>
</group>
<group>
<val>3</val>
<val>2</val>
<val>2</val>
</group>
Now in my XSL transform I want to define 1 variable “highestsum”, which contains the highest sum of ‘values’. So for the example, it would return 7, the sum of all values in the second group.
After some searching, this is the closest solution I found:
http://w3schools.invisionzone.com/index.php?showtopic=24265
But I have a feeling that there’s a better way than using sorting in a template to achieve this result. Any takers?
I. A good XSLT 1.0 solution (brief, efficient and understandable):
when this transformation is applied on the following XML document:
the wanted, correct result is produced:
To get the desired variable definition, simply put the
<xsl:for-each>instruvtion from the above code in the body of the variable.II. An even better XSLT 2.0 (and actually XPath 2.0 one-liner) solution:
when this transformation is applied on the same XML document, the same correct answer is produced:
And the wanted variable definition is simply:
Finally, the same Xpath expression can be used in XQuery to define the required variable: