Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 有没有一种方法可以在xslt上将字符串计算为操作?_Xml_String_Xslt - Fatal编程技术网

Xml 有没有一种方法可以在xslt上将字符串计算为操作?

Xml 有没有一种方法可以在xslt上将字符串计算为操作?,xml,string,xslt,Xml,String,Xslt,我想将字符串变量的内容作为一个操作进行计算 我的变量是以下字符串: 1+1+0 我想进行运算,得到值2 提前感谢,, Gustavo在XSLT2.0中,这可以通过使用tokenize()、number()和sum()函数来实现,例如下面的函数。注意,我在+符号周围包含了可能的空格 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/19

我想将字符串变量的内容作为一个操作进行计算

我的变量是以下字符串: 1+1+0

我想进行运算,得到值2

提前感谢,,
Gustavo

在XSLT2.0中,这可以通过使用
tokenize()
number()
sum()
函数来实现,例如下面的函数。注意,我在
+
符号周围包含了可能的空格

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:variable name="test1">1+1+0</xsl:variable>
    <xsl:variable name="test2"> 3 + 
    5   +           12 </xsl:variable>

    <xsl:template match="/">
        <xsl:variable name="testSplit1" select="tokenize($test1,'\s*\+\s*')"/>
        <xsl:variable name="testSplitNum1">
            <xsl:for-each select="$testSplit1">
                <item><xsl:value-of select="number(.)"/></item>
            </xsl:for-each>
        </xsl:variable>

        <xsl:variable name="testSplit2" select="tokenize($test2,'\s*\+\s*')"/>
        <xsl:variable name="testSplitNum2">
            <xsl:for-each select="$testSplit2">
                <item><xsl:value-of select="number(.)"/></item>
            </xsl:for-each>
        </xsl:variable>

        <root>
            <items1><xsl:copy-of select="$testSplitNum1"/></items1>
            <sum1><xsl:value-of select="sum($testSplitNum1/item)"/></sum1>
            <items2><xsl:copy-of select="$testSplitNum2"/></items2>
            <sum2><xsl:value-of select="sum($testSplitNum2/item)"/></sum2>
        </root>

    </xsl:template>

</xsl:stylesheet>

1+1+0
3 + 
5   +           12 
结果(使用虚拟输入文件)为


1.
1.
0
2.
3.
5.
12
20

XSLT 1.0或XSLT 2.0中没有通用机制来计算作为字符串值提供的XPath表达式。许多产品都有这样的扩展(例如,请参见saxon:evaluate())。XSLT 3.0中引入了一种通用机制:xsl:evaluate指令

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <items1>
        <item>1</item>
        <item>1</item>
        <item>0</item>
    </items1>
    <sum1>2</sum1>
    <items2>
        <item>3</item>
        <item>5</item>
        <item>12</item>
    </items2>
    <sum2>20</sum2>
</root>