Introduction


Chapter 2 lamented the absence of sophisticated string-processing facilities in native XSLT 1.0. 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.

In XPath/XSLT 2.0, some of the more obvious math functions have been added including abs( ), avg( ), max( ), min( ), round-to-half-even( ). Further, there are now more numerical types (xs:integer, xs:double, and the other numeric types defined in XML Schema datatypes) where XSLT 1.0 only had a single floating point numeric type. Thankfully, XPath 2.0 now recognizes scientific notation, whose absence was often a major inconvenience in 1.0.

However, whether you are using 1.0 or 2.0, do not expect to find matrix multiplication or Fast-Fourier transform recipes in this section. If you really need to perform sophisticated math on XML-encoded data, then XSLT is not the language for you. Instead, bring the data into a more mathematically inclined language using an XSLT front-end 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 XSLT facilities that are sometimes misunderstood.

Many of the recipes shown here are implementations of EXSLT's math definitions. When a pure XSLT implementation is available at EXSLT.org, we will discuss that first and then consider alternative solutions.[1] Pure XSLT implementations are provided for all extensions defined in EXSLT math except for trigonometric functions (sin, cos, etc.). If you desperately need a pure XSLT implementation of trigonometric functions, then Recipe 3.5 will point you in one general direction.

[1] The EXSLT implementations shown are as they existed at the time of this writing. Naturally, these may be improved over time or deprecated as new versions of XPath and XSLT emerge.

Many discussion sections in this chapter will explore alternative implementations of the solution. Readers uninterested in technical details are encouraged simply to use the example shown in the solution section since it will be the best solution or as good as the alternatives.




XSLT Cookbook
XSLT Cookbook: Solutions and Examples for XML and XSLT Developers, 2nd Edition
ISBN: 0596009747
EAN: 2147483647
Year: 2003
Pages: 208
Authors: Sal Mangano

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net