[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: sqrt

Subject: Re: sqrt
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Mon, 13 Aug 2001 15:48:18 +0100
saxon script
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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.