xslt-2.0 - 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>
解决方案
有 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 以来的所有版本)。
推荐阅读
- regex - 为什么这个 sed 正则表达式替换不起作用?
- python - 在循环中运行 sqlite 查询会在 20 次调用后变慢
- xamarin - 为什么单击按钮时活动指示器需要将近 3 秒才能启动
- hbase - flink sink 失败时,sink 中的缓冲区会丢失吗?
- windows - Windows - 如何在 SourceTree 中重新定位工作副本(移动本地存储库)?
- laravel - 如何仅忽略标题形式的第一页
- python - jay 文件格式是否特定于 Python 数据表?
- collections - Binding 现已弃用的 Collection 一致性的干净替代方案?
- python-3.x - 尝试编写文本换行函数,但代码在所需输出后显示几个空行
- apache-spark-sql - 当 arrays_zip 数据源自嵌套时,Spark DataFrame 的输出将列名替换为“0”、“1”