Chapter 2. Numbers and Math

Arithmetic is being able to count up to twenty without taking off your shoes.

Mickey Mouse

Chapter 1 lamented the absence of sophisticated string-processing facilities in native XSLT. By comparison, XSLT’s handling of numerical computation is truly “Mickey Mouse”! XSLT 1.0 gives you facilities for basic arithmetic, counting, summing, and formatting numbers, but the remaining mathematics is up to your sheer wit. Fortunately, as with strings, XSLT’s recursive powers permit reasonable mathematical feats with reasonable effort.

Do not expect to find matrix multiplication or Fast-Fourier transform recipes in this section. If you really need them to perform on XML-encoded data then XSLT is not the language for you. Instead, bring the data into a C, C++, or Fortran program using an XSLT frontend converter or native SAX or DOM interface. Nevertheless, a web page called “Gallery of Stupid XSL and XSLT Tricks” (http://www.incrementaldevelopment.com/xsltrick/) contains some interesting mathematical XSLT curiosities such as computing primes and differentiating polynomials. These tricks can be instructive because they might extend your understanding of XSLT. Instead, this chapter concentrates on recipes that demonstrate commonly used mathematics that can be implemented economically within the confines of XSLT.

Some of this section’s early examples read more like tutorials on how to use native functionality in XSLT. I include these examples because they represent ...

Get XSLT Cookbook 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.