首页 > 解决方案 > saxon9HE 中的指数或幂计算

问题描述

请建议如何使用 Saxon 9HE 在 XSLT2 中执行诸如power之类的数学函数。

收到以下错误: Cannot find a matching 2-argument function named {http://exslt.org/math}power()

XML:

<root><num>12.3</num></root>

XSLT 2.0:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
xmlns:math="http://exslt.org/math"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
extension-element-prefixes="exsl math">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>

<!--1/(1+e^(-t))--><!-- this is required formula -->

<xsl:template match="num">
    <xsl:variable name="varE"><xsl:value-of select="."/></xsl:variable>
    <xsl:variable name="varT"><xsl:text>0.718</xsl:text></xsl:variable>
    <xsl:variable name="varPower">
     <xsl:value-of select="1 div (1 + math:power(number($varE), number(-$varT)))"/>
    </xsl:variable>

    <xsl:value-of select="$varPower"/>
</xsl:template>

</xsl:stylesheet>

标签: xslt-2.0

解决方案


有 XPath 标准化math函数,math:pow例如math:pow(2, 4)在命名空间https://www.w3.org/2005/xpath-functions/math中,例如在 Saxon 的所有版本中都可用的命名空间声明xmlns:math="http://www.w3.org/2005/xpath-functions/math"(至少在 9.8 中,但我认为它也适用于更早的版本,如 9.7 和 9.6(文档http://saxonica.com/html/documentation9.6/functions/math/pow.html表示自 9.6 以来的所有版本)。


推荐阅读