Calculate a square root of a number in XSL

1k views Asked by At

I have attempted several iterations of calculating a square root of a number in XSL/XML but can't get it working. This is my latest attempt. I am getting an error "Named template 'sqrt' does not exist in the stylesheet." This is the last bit that I need to write for this stylesheet and then I can turn my project in. -TIA

<?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">
<xsl:import href="sqrt.xsl" />

<xsl:template match="HorizontalLine | HorizontalCircle | HorizontalSpiral">
<xsl:if test="Start[@pointType = 'POB']">
    SqRt:
      <xsl:call-template name="sqrt">
        <xsl:with-param name="value" select="100"/>
      </xsl:call-template>
</xsl:if>
</xsl:template>
2

There are 2 answers

0
Michael Madrid On
<?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:import href="sqrt.xsl" />
<xsl:include href="../format.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="25"/>
                </xsl:call-template>
            </td>
            <!-- End Station -->
        </tr>
</xsl:template>

7
Michael Kay On

The error message says that "named template 'sqrt' doesn't exist". We would expect to find it in sqrt.xsl, but you haven't shown us sqrt.xsl, so we can infer (a) that it's not there, and (b) that you probably don't understand how import works, otherwise you would have understood the message and shown us the code.

For an example of how to calculate square roots in XSLT 1.0, see http://exslt.org/math/functions/sqrt/math.sqrt.template.xsl

Two other comments:

(a) there is no XSLT version 1.1. There was a draft of 1.1 (round about 2001) but it was abandoned and never got beyond draft status.

(b) you say: "This is the last bit that I need to write for this stylesheet and then I can turn my project in". (i) This information is irrelevant to your question, and (ii) it reflects your inexperience, since an experienced programmer knows that you should never expect the bug you are currently working on to be the last one.