【发布时间】:2015-07-25 11:16:42
【问题描述】:
在以下代码中:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="input">
<xsl:variable name="n" select="tokenize(./text(),'[0-9]+([,.][0-9]+)?')"/>
<xsl:choose>
<xsl:when test="(($n[0]*$n[0]+$n[1]*$n[1])/(n[3]*($n[0]*$n[0]+$n[1]*$n[1])))>300">
Retina!
</xsl:when>
<xsl:otherwise>
Trash!
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
示例输入:
<input>1920 1400 0.425</input>
我使用tokenize 来匹配两个input 标签之间的一些数字。唯一的问题是生成的数字列表都被格式化为字符串,因此后面的代码中的数学运算失败了。
那么,有没有办法让标记器输出数字列表,或者在 XSLT 2.0 中是否有与 map 等效的方法?
注意:我使用的是 Saxon 处理器
【问题讨论】:
-
您能否发布一个输入示例,以便我们重现您的问题?
-
@michael.hor257k 当然
-
考虑解释你想要实现什么,你想为你发布的输入创建哪个输出?您的代码没有向我解释这一点,因为
$n[0]在 XSLT/XPath 中没有意义,其中序列中的项目索引以1开头。并且要使用您的输入1920 1400 0.425获取数字序列,我只希望使用tokenize(., '\s+'),如果您使用与数字匹配的正则表达式作为第二个参数,则返回的序列包含数字之间的字符串。
标签: xml xslt string-formatting number-formatting integer-arithmetic