XML technologies / XSLT / XSLT Cookbook / Numbers and calculations
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
“Mathematics lets you count to 20 without taking off your shoes.”
Mickey Mouse
Introduction
In Strings I lamented native XSLT 1.0’s lack of advanced string-processing facilities. By comparison, its mathematical capabilities are truly weak! XSLT 1.0 provides basic arithmetic, counting, summing and number formatting, but leaves further mathematics to your ingenuity. Fortunately, as with strings, recursion lets you implement reasonable mathematical functions with reasonable effort.
In XPath/XSLT 2.0 some obvious mathematical functions were added, including abs( ), avg( ), max( ), min( ) and round-to-half-even( ). There are also further numeric types (xs:integer, xs:double and the other numeric types defined in XML Schema — data types ), whereas XSLT 1.0 had only a single floating-point type. Thankfully, XPath 2.0 now recognizes scientific notation, whose absence was one of 1.0’s greater inconveniences.
Whether using 1.0 or 2.0, do not expect recipes for matrix multiplication or fast Fourier transforms here. If you need advanced mathematics on XML-encoded data, XSLT is not the right language. Instead, move the data to a language better suited to mathematics, using an XSLT front-end converter or the native SAX or DOM interface. Nevertheless, a webpage called “Gallery of Stupid XSL and XSLT Tricks” offers interesting mathematical XSLT curiosities, such as finding primes and differentiating polynomials. These tricks can be instructive and expand your understanding of XSLT. This chapter instead concentrates on recipes demonstrating common calculations that can be implemented economically within XSLT’s limits.
Some early examples read more like instructions for using XSLT’s built-in features. They are included because they clarify features that are sometimes misunderstood.
Many recipes implement EXSLT’s mathematical definitions. When EXSLT.org provides a pure XSLT implementation, we discuss it first, then consider alternatives. Note that these EXSLT implementations are shown as they existed when the book was written; they may improve over time or become obsolete with new XPath and XSLT versions.
Pure XSLT implementations are supplied for all EXSLT mathematics extensions except trigonometric functions (sin, cos, etc.). If you absolutely need a pure XSLT trigonometric implementation, recipe Implementing common mathematical functions points you in the right direction.
Many discussion sections explore alternative implementations. Readers uninterested in technical details can simply use the example in the solution, which is either the best approach or at least as good as its alternatives.
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XSLT Kochbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de