如何在 xslt 1.0 中做平方根 [英] How to do square root in xslt 1.0

查看:29
本文介绍了如何在 xslt 1.0 中做平方根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 xslt 文件中做一些计算,需要在公式中做一些平方根.有人可以指出它是否可能以及如何吗?

I want to do some calculation within the xslt file and need to do some squre root in the formula. can someone please point out if it is possible and how please?

感谢磨坊

推荐答案

可以使用FXSL:

One can use the sqrt template/function of FXSL:

我.在 XSLT 1.0 中:

I. In XSLT 1.0:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:import href="sqrt.xsl"/>

  <!-- To be applied on any source xml. 
       This also tests the within() function 
  -->

  <xsl:output indent="yes" omit-xml-declaration="yes"/>

  <xsl:template match="/">
     sqrt(0.25): 
     <xsl:call-template name="sqrt">
        <xsl:with-param name="N" select="0.25"/>
        <xsl:with-param name="Eps" select="0.00001"/>
     </xsl:call-template>

     sqrt(1): 
     <xsl:call-template name="sqrt">
        <xsl:with-param name="N" select="1"/>
        <xsl:with-param name="Eps" select="0.00001"/>
     </xsl:call-template>

     sqrt(2): 
     <xsl:call-template name="sqrt">
        <xsl:with-param name="N" select="2"/>
        <xsl:with-param name="Eps" select="0.00001"/>
     </xsl:call-template>

     sqrt(9): 
     <xsl:call-template name="sqrt">
        <xsl:with-param name="N" select="9"/>
        <xsl:with-param name="Eps" select="0.00001"/>
     </xsl:call-template>

     sqrt(16): 
     <xsl:call-template name="sqrt">
        <xsl:with-param name="N" select="16"/>
        <xsl:with-param name="Eps" select="0.00001"/>
     </xsl:call-template>
     sqrt(25): 
     <xsl:call-template name="sqrt">
        <xsl:with-param name="N" select="25"/>
        <xsl:with-param name="Eps" select="0.00001"/>
     </xsl:call-template>
     sqrt(36): 
     <xsl:call-template name="sqrt">
        <xsl:with-param name="N" select="36"/>
        <xsl:with-param name="Eps" select="0.00001"/>
     </xsl:call-template>

     sqrt(49): 
     <xsl:call-template name="sqrt">
        <xsl:with-param name="N" select="49"/>
        <xsl:with-param name="Eps" select="0.00001"/>
     </xsl:call-template>

     sqrt(64): 
     <xsl:call-template name="sqrt">
        <xsl:with-param name="N" select="64"/>
        <xsl:with-param name="Eps" select="0.00001"/>
     </xsl:call-template>

     sqrt(81): 
     <xsl:call-template name="sqrt">
        <xsl:with-param name="N" select="81"/>
        <xsl:with-param name="Eps" select="0.00001"/>
     </xsl:call-template>

     sqrt(100): 
     <xsl:call-template name="sqrt">
        <xsl:with-param name="N" select="100"/>
        <xsl:with-param name="Eps" select="0.00001"/>
     </xsl:call-template>

     sqrt(121): 
     <xsl:call-template name="sqrt">
        <xsl:with-param name="N" select="121"/>
        <xsl:with-param name="Eps" select="0.00001"/>
     </xsl:call-template>
  </xsl:template>

</xsl:stylesheet>

当此转换应用于任何 XML 文档(未使用)时,会产生想要的正确结果:

 sqrt(0.25): 
 0.5000000795866174

 sqrt(1): 
 1.0000000464611474

 sqrt(2): 
 1.4142156862745097

 sqrt(9): 
 3.0000000000393214

 sqrt(16): 
 4.0000001858445895
 sqrt(25): 
 5.000000000016778
 sqrt(36): 
 6.000000002793968

 sqrt(49): 
 7.000000094930961

 sqrt(64): 
 8.000001273385879

 sqrt(81): 
 9.000009415515176

 sqrt(100): 
 10.000000000107445

 sqrt(121): 
 11.000000001323027

<小时>

二.将 FXSL 2.x 用于 XSLT 2.0:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:f="http://fxsl.sf.net/">
  <xsl:import href="../f/func-sqrt.xsl"/>
  <xsl:import href="../f/func-map.xsl"/>
  <xsl:import href="../f/func-standardXpathFunctions.xsl"/>
    <xsl:output method="text"/>

 <xsl:template match="/">
     <xsl:value-of  separator="&#xA;" select=
     "f:map(f:round-half-to-even(f:sqrt(2, 0.000001)),
            0 to 13
          )
   "/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于任何 XML 文档(未使用)时,会产生所需的正确结果:

1
1.4
1.41
1.414
1.4142
1.41421
1.414214
1.4142136
1.41421356
1.414213562
1.4142135624
1.41421356237
1.414213562375
1.4142135623747

这篇关于如何在 xslt 1.0 中做平方根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆