Doing Math Without Recursion
Our original stylesheet had to use recursion to
calculate the total of each purchase order. We passed all of the
<item>
elements to a recursive template, which returned the total for
the purchase order after processing all of the <item>
s. In XSLT 2.0, we can use a
simple XPath expression to do all that work for us. Here’s the single
line of code:
<xsl:value-of select="format-number(sum(items/item/(qty * price)), '$#,###.00')"/>
That’s it. XPath 2.0’s support for mathematical expressions inside path expressions makes this ridiculously easy. We replaced maybe 20 lines of code with just 1.
Get XSLT, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.