Subject: [xsl] RE: sqrt ("pure" XSLT solution) From: Nate Austin <naustin@xxxxxxxxxx> Date: Mon, 13 Aug 2001 14:26:40 -0500 |
> Date: Mon, 13 Aug 2001 00:44:34 -0700 (PDT) > From: edouard panie <edpanie@xxxxxxxxx> > Subject: [none] > > Hi, > > I need to use a sqrt() math function in a XSL/SVG > style sheet. Knowing that I use a Saxon processor > > Here is the header I had before (when it worked!, but > with out sqrt()): > > > <?xml version="1.0" encoding="UTF-8"?> > <xsl:stylesheet version="1.0" > extension-element-prefixes="saxon" > xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > xmlns:saxon="http://icl.com/saxon"> > > > 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? > > > > <?xml version="1.0" encoding="utf-8"?> > <stylesheet > xmlns="http://www.w3.org/1999/XSL/Transform" > xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > xmlns:saxon="http://icl.com/saxon" > xmlns:func="http://exslt.org/functions" > xmlns:math="http://exslt.org/math" version="1.0" > extension-element-prefixes="math func saxon" > math:doc="http://www.exslt.org/math"> > > <import href="math.sqrt.msxsl.xsl"/> > <func:script language="exslt:javascript" > implements-prefix="math" src="math.sqrt.js"/> > <script implements-prefix="math" src="math.sqrt.js" > language="javascript"/> > </stylesheet> > > > > > Thanks for your help > Ed Ed - Here's a solution to your problem using an implementation of Newton's method for finding square roots. Benefit: Processor independant (no extension functions) Drawback: Probably slightly slower, at times will leave a number hanging on at the end (using the example I have here, 2761.5025 should return 52.55 but instead 52.550000000000004 is returned. This will mostly go away with a higher 'max iteration' count, but there may always be some special cases that should be rounded off.) Call it as shown below: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html><body> <xsl:call-template name="sqrt"> <xsl:with-param name="num" select="2761.5025"/> </xsl:call-template> </body> </html> </xsl:template> <xsl:template name="sqrt"> <xsl:param name="num" select="0"/> <!-- The number you want to find the square root of --> <xsl:param name="try" select="1"/> <!-- The current 'try'. This is used internally. --> <xsl:param name="iter" select="1"/> <!-- The current iteration, checked against maxiter to limit loop count --> <xsl:param name="maxiter" select="10"/> <!-- Set this up to insure against infinite loops --> <!-- This template was written by Nate Austin using Sir Isaac Newton's method of finding roots --> <xsl:choose> <xsl:when test="$try * $try = $num or $iter > $maxiter"> <xsl:value-of select="$try"/> </xsl:when> <xsl:otherwise> <xsl:call-template name="sqrt"> <xsl:with-param name="num" select="$num"/> <xsl:with-param name="try" select="$try - (($try * $try - $num) div (2 * $try))"/> <xsl:with-param name="iter" select="$iter + 1"/> <xsl:with-param name="maxiter" select="$maxiter"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> Here's the XML file I used to test it with IE5.5 + MSXML4 in replace mode: <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="sqrt.xsl"?> <nub/> Hope that helps, Nate naustin@xxxxxxxxxx XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] XPath statement required , Sanjay Pandey/Towers | Thread | Re: [xsl] RE: sqrt ("pure" XSLT sol, Kurt Cagle |
Re: [xsl] n-value of preceding lb-e, Thomas B. Passin | Date | Re: [xsl] RE: sqrt ("pure" XSLT sol, Kurt Cagle |
Month |