XSLT 2.0 Advanced calculation includes Cos() Sin()

1.2k views Asked by At

As I know there's no out of box XSLT function for advanced calculation, anyone have any idea how I can do math such as Cosine/Sin() within XSLT? The requirement is also very specific which I need the extension to be downloadable(instead of pointing to a url) so that I can download it and reference as source within my XSL script. I am unable to download EXSLT source files to try as it seems like there's an issue with that page. Can you please also provide a simple example of how to reference the source within the script and calculate something simple like:

<input>
<num1>1</num1>
<num2>1</num2>
</input>

and have the output as:

<output>
<cosOfnum1>0.54030230586</cosOfnum1>
<sinOfnum2>0.8414709848</sinOfnum2>
</output>

Thank you!

2

There are 2 answers

0
Martin Honnen On BEST ANSWER

Saxon 9 (including HE, tested with 9.6, see also http://saxonica.com/html/documentation9.6/functions/math/sin.html saying "From Saxon 9.6, available in all editions.") does have out of the box support for the functions defined in https://www.w3.org/2005/xpath-functions/math/, here is an example:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    exclude-result-prefixes="xs math"
    version="2.0">

    <xsl:template match="input">
        <output>
            <xsl:apply-templates/>
        </output>
    </xsl:template>

    <xsl:template match="num1">
        <cosOfnum1>
            <xsl:value-of select="math:cos(.)"/>
        </cosOfnum1>
    </xsl:template>

    <xsl:template match="num2">
        <sinOfnum2>
            <xsl:value-of select="math:sin(.)"/>
        </sinOfnum2>
    </xsl:template>

</xsl:stylesheet>

I have also tested that XmlPrime 4 runs the above stylesheet correctly when using the command line option --xt30 so it also has built-in support for those functions.

Altova XMLSpy 2017 also supports the functions in the namespace xmlns:math="http://www.w3.org/2005/xpath-functions/math" out of the box but only in version="3.0" stylesheets.

0
Dimitre Novatchev On

As I know there's no out of box XSLT function for advanced calculation, anyone have any idea how I can do math such as Cosine/Sin() within XSLT?

Using the FXSL library, one can have among other things trigonometric functions, written in pure XSLT 1.0 or XSLT 2.0:

http://fxsl.sourceforge.net/articles/xslCalculator/The%20FXSL%20Calculator.html#The_Trigonometric_Functions

The Math modules of FXSL implement:

  1. The wellknown trigonometric functions: sin(), cos(), tan(), cot(), sec(), csc()
  2. Hyperbolic trigonometric functions: hsin(), hcos(), htan(), hcot(), hsec(), hcsc()
  3. Exponential and logarithmic functions: exp(), ln(), log10(), log2(), log(), pow(), sqrt()
  4. Solving equations of one real variable: the Newton - Raphson's method and the Binary Search method.
  5. Inverse functions: arcsin(), arccos(), arctan(), arccot(), arcsec(), arccsc(), archsin(), archcos(), archtan(), archcot(), archsec(), archcsc(),