XML Square Root function

prostang
prostang used Ask the Experts™
on
I have a XSL that I have put together and it's about done.  I need to calculate the square root of a number.  I have researched and found what I THOUGHT was the solution, but I could not get it to work.  I need assistance putting it together.  I am getting an error that is attached.  XML errorHere is the code I have:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:inr="http://mycompany.com/mynamespace" xmlns:math="http://exslt.org/math/functionssqrt/math.sqrt.template.xsl" extension-element-prefixes="math">
    <xsl:include href="../format.xsl"/>
	<xsl:import href="sqrt.xsl" />
    <xsl:param name="xslRootDirectory" select="inr:xslRootDirectory"/>
    <!-- List Coordinates with Station Report -->
    <xsl:template match="/">
        <xsl:variable name="gridOut" select="inr:SetGridOut(number(InRoads/@outputGridScaleFactor))" />
        <html>
            <head>
                <link rel="stylesheet" type="text/css" href="{$xslRootDirectory}/_Themes/engineer/theme.css"/>
            </head>
            <body>
				<xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>
    <!-- Element Point Data -->
    <xsl:template match="HorizontalLine | HorizontalCircle | HorizontalSpiral">
        <xsl:variable name="startStation" select="Start/station/@internalStation"/>
        <xsl:variable name="endStation" select="End/station/@internalStation"/>
		<!-- This section is to force the PC to PI information -->
			<tr>
				<xsl:variable name="varElementNumber" select="@elementNumber - 1"/>
				<!-- Element DIST Section -->
				<xsl:variable name="varX1" select="PI/@easting"/>
				<xsl:variable name="varY1" select="PI/@northing"/>
				<xsl:variable name="varX2" select="Start/@easting"/>
				<xsl:variable name="varY2" select="Start/@northing"/>
				<td class="sidepad" align="center">
				varElementNumber: <xsl:value-of select = "$varElementNumber"/><br/>
				varX1: <xsl:value-of select = "$varX1"/><br/>
				varY1: <xsl:value-of select = "$varY1"/><br/>
				varX2: <xsl:value-of select = "$varX2"/><br/>
				varY2: <xsl:value-of select = "$varY2"/><br/>
				Distance: <xsl:value-of select = "($varX2 - $varX1) * ($varX2 - $varX1) + ($varY2 - $varY1) * ($varY2 - $varY1)"/><br/>
				SqRt: 	
					<xsl:call-template name="math:sqrt">
						<xsl:with-param name="value" select="100"/>
					</xsl:call-template>
				</td>
				<!-- End Station -->
			</tr>
    </xsl:template>
</xsl:stylesheet>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
What is the content of the "sqrt.xsl" file?

Author

Commented:
I corrected the error in the path where I was missing a "/".  I still get the error.  This is the correct path:  http://exslt.org/math/functions/sqrt/math.sqrt.template.xsl


<?xml version="1.0"?>
<xsl:stylesheet extension-element-prefixes="math" xmlns:math="http://exslt.org/math" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template name="math:sqrt">
<!-- The number you want to find the square root of -->
<xsl:param name="number" select="0"/>
<!-- The current 'try'. This is used internally. -->
<xsl:param name="try" select="1"/>
<!-- The current iteration, checked against maxiter to limit loop count -->
<xsl:param name="iter" select="1"/>
<!-- Set this up to ensure against infinite loops -->
<xsl:param name="maxiter" select="10"/>
<!-- This template was written by Nate Austin using Sir Isaac Newton's method of finding roots -->
<xsl:choose>
<xsl:when test="$try * $try = $number or $iter > $maxiter">
<xsl:value-of select="$try"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="sqrt">
<xsl:with-param name="number" select="$number"/>
<xsl:with-param name="try" select="$try - (($try * $try - $number) 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>

Open in new window

Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
The namespace declaration for math: needs to be the same in both xsl files
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Gertone (Geert Bormans)Information Architect
Top Expert 2006
Commented:
xmlns:math="http://exslt.org/math"
Is what you also need in the calling xsl
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
Xmlns:math declares the namespace the function is in,
There is no relation to the url the xsl is found in
Gertone (Geert Bormans)Information Architect
Top Expert 2006
Commented:
xmlns:math="http://exslt.org/math/functionssqrt/math.sqrt.template.xsl"
 In the calling xslt is wrong

Author

Commented:
So by changing the first two lines to the following, I still get the error.  Please keep in mind that XML is not my comfort zone, so terminology is lost on me.  I do have an XSLT book next to me that I reference.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:inr="http://mycompany.com/mynamespace" xmlns:math="http://exslt.org/math" extension-element-prefixes="math:sqrt">

Open in new window

Gertone (Geert Bormans)Information Architect
Top Expert 2006
Commented:
Also drop this
extension-element-prefixes="math:sqrt"

Author

Commented:
This is what I was attempting to follow:  http://exslt.org/math/functions/sqrt/math.sqrt.html
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
But you don't get the same error
At most you get a similar error with the different namespace
If that is not the case you are calling a cache of the old file

Make sure the file reference of the xsl:import is correct
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
I am. very  well aware of the marh:sqrt extensions, but this is the template version, so no need for the extension element

Author

Commented:
ERROR:  "Named template '{http://exslt.org/math}sqrt' does not exist in the stylesheet."
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
Right, it does not find the imported file
Gertone (Geert Bormans)Information Architect
Top Expert 2006
Commented:
Sqrt.xsl needs to be in the same folder

Author

Commented:
but if I type in the url, it opens and I can read the contents.
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
Ah wrong parameter name

<xsl:with-param name="value" select="100"/>

Needs to be

                                    <xsl:with-param name="number" select="100"/>
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
Have you copied the file with the named template to a local file sqrt.xsl ?

Author

Commented:
Ok.  Copied the sqrt.xsl into the same folder as my xsl and still get same error.  It is attempting to read it from the URL.  

Made the modification to the with-param call.
Gertone (Geert Bormans)Information Architect
Top Expert 2006
Commented:
Another issue
Xsl:import needs to come before xsl:include

I wonder why your xslt processor does not report that

What processor are you using?

Author

Commented:
This is the stylesheet right now:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:inr="http://mycompany.com/mynamespace" xmlns:math="http://exslt.org/math">
    <xsl:include href="../format.xsl"/>
	<xsl:import href="sqrt.xsl" />
    <xsl:param name="xslRootDirectory" select="inr:xslRootDirectory"/>
    <!-- List Coordinates with Station Report -->
    <xsl:template match="/">
        <xsl:variable name="gridOut" select="inr:SetGridOut(number(InRoads/@outputGridScaleFactor))" />
        <html>
            <head>
                <link rel="stylesheet" type="text/css" href="{$xslRootDirectory}/_Themes/engineer/theme.css"/>
            </head>
            <body>
				<xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>
    <!-- Element Point Data -->
    <xsl:template match="HorizontalLine | HorizontalCircle | HorizontalSpiral">
        <xsl:variable name="startStation" select="Start/station/@internalStation"/>
        <xsl:variable name="endStation" select="End/station/@internalStation"/>
		<!-- This section is to force the PC to PI information -->
			<tr>
				<xsl:variable name="varElementNumber" select="@elementNumber - 1"/>
				<!-- Element DIST Section -->
				<xsl:variable name="varX1" select="PI/@easting"/>
				<xsl:variable name="varY1" select="PI/@northing"/>
				<xsl:variable name="varX2" select="Start/@easting"/>
				<xsl:variable name="varY2" select="Start/@northing"/>
				<td class="sidepad" align="center">
				varElementNumber: <xsl:value-of select = "$varElementNumber"/><br/>
				varX1: <xsl:value-of select = "$varX1"/><br/>
				varY1: <xsl:value-of select = "$varY1"/><br/>
				varX2: <xsl:value-of select = "$varX2"/><br/>
				varY2: <xsl:value-of select = "$varY2"/><br/>
				Distance: <xsl:value-of select = "($varX2 - $varX1) * ($varX2 - $varX1) + ($varY2 - $varY1) * ($varY2 - $varY1)"/><br/>
				SqRt: 	
					<xsl:call-template name="math:sqrt">
						<xsl:with-param name="number" select="100"/>
					</xsl:call-template>
				</td>
				<!-- End Station -->
			</tr>
    </xsl:template>
</xsl:stylesheet>

Open in new window

Author

Commented:
I am using an application that processes civil engineering reports.  I am now getting an error stating:
 "Named template 'sqrt' does not exist in the stylesheet"
Information Architect
Top Expert 2006
Commented:
sorry for the somewhat slower feedback, but I really needed to go to bed

on line 25 of the sqrt function in the file you import is a bug in my opinion

                <xsl:call-template name="sqrt">
should be
                <xsl:call-template name="math:sqrt">

it is weird noone ever fixed that
(few people use a processor that does not allow any of the other sqrt() options actually, that is why I asked that earlier)

Author

Commented:
Thank you SO MUCH Geert!  You put in a lot of analyzing and work to solve my question.   Thanks again!!!
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
welcome

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial