This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: sqrt


Hi Ed,

> I need to use a sqrt() math function in a XSL/SVG style sheet.
> Knowing that I use a Saxon processor
>
> I tried with EXSLT, I downloaded math.sqrt.msxsl.xsl and
> math.sqrt.js, put them in the same directory as my xsl stylesheet
> but it doesn't work! This is what I tried, but when I run the saxon
> processor, it gives errors: the processor doesn't recognize the SVG
> tags anymore! I don't understand what's wrong, can someone give me a
> hint?

As you might guess from the name, math.sqrt.msxsl.xsl is designed for
MSXML - it uses msxsl:script to access the JavaScript implementation
of the math:sqrt() function. Saxon does not understand msxsl:script so
this stylesheet will not work with Saxon.

math.sqrt.js is a JavaScript file that implements the math:sqrt()
function. Theoretically, you could tell Saxon to use it with the
xsl:script element, since Saxon implements the XSLT 1.1 Working Draft.
However, Saxon does not support functions implemented in JavaScript,
so again this JavaScript implementation will not work with Saxon. The
only language that you can write extension functions in to get them to
work with Saxon is Java.

Saxon also does not natively support math:sqrt() as yet, and does not
have its own saxon:sqrt() function.

Therefore, you have to define "your own" Java extension function. You
can do this using the Saxon-only method:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:math="java:java.lang.Math"
                extension-element-prefixes="math">

  ... math:sqrt(...) ...
                
</xsl:stylesheet>

This is not the best method to use because it ties you to Saxon and
uses a method that is unlikely to be similar to the standard method
that will be available in XSLT 2.0.

You could combine this with the EXSLT method of defining functions
with func:function:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:javaMath="java:java.lang.Math"
                xmlns:math="http://exslt.org/math";
                xmlns:func="http://exslt.org/functions";
                extension-element-prefixes="javaMath math func">

<func:function name="math:sqrt">
  <xsl:param name="number" />
  <func:result select="javaMath:sqrt(number($number))" />
</func:function>
                
  ... math:sqrt(...) ...
                
</xsl:stylesheet>

Or you could use the XSLT 1.1 method as follows:

<xsl:stylesheet version="1.1"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:math="http://exslt.org/math";
                extension-element-prefixes="math">

<xsl:script language="java"
            implements-prefix="math"
            src="java:java.lang.Math" />
                
  ... math:sqrt(...) ...
                
</xsl:stylesheet>

This has the advantage that other processors that support XSLT 1.1 and
Java implementations will also be able to use the stylesheet.

Or you could use the Saxon-specific XSLT 1.1-like method:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:math="http://exslt.org/math";
                xmlns:saxon="http://icl.com/saxon";
                extension-element-prefixes="math saxon">

<saxon:script language="java"
              implements-prefix="math"
              src="java:java.lang.Math" />
                
  ... math:sqrt(...) ...
                
</xsl:stylesheet>

With the latter three, be aware that while you're using the EXSLT -
Maths namespace, the implementation that you're getting is direct from
Java. In this case that doesn't really matter, since sqrt() is hardly
likely to be different as defined in EXSLT - Math than it is in Java,
but in other cases it might. EXSLT *should* have a org.exslt.Math Java
class that implements them properly, but we don't at the moment.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]