Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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之前使用带子字符串的sum_Xml_Xslt - Fatal编程技术网

Xml 在XSLT之前使用带子字符串的sum

Xml 在XSLT之前使用带子字符串的sum,xml,xslt,Xml,Xslt,我有一个包含多个帐户的XML,我正在尝试使用score/max_score格式计算多个分数的总和 XML: 1. 詹姆斯 50/100 2. 卡蒂 10/100 3. 阿拉伯树胶 30/100 4. 詹姆斯 50/100 5. 快跑5 40/100 和XSLT: <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

我有一个包含多个帐户的XML,我正在尝试使用score/max_score格式计算多个分数的总和

XML:


1.
詹姆斯
50/100
2.
卡蒂
10/100
3.
阿拉伯树胶
30/100
4.
詹姆斯
50/100
5.
快跑5
40/100
和XSLT:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:template match="/">
        <html>
            <body>

                <p>
                    <xsl:value-of select="sum(//accounts/account/score/number(substring-before(.,'/')))"/>  
                </p>

            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>



但是,当我运行xml时,它表示有错误,并且不返回总和。为什么?

问题是您的XSLT 2.0转换在web浏览器中不起作用,web浏览器本身只支持XSLT 1.0

有关XSLT1.0中对元素求和的方法,请参阅Michael Kay对的回答,以了解一般思路。有关一些代码示例,请参见Dimitre Novatchev的答案。有关web浏览器中XSLT2.0的实际支持,请参阅

我喜欢递归方法。以下是它如何应用于您的问题:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="/">
    <html>
      <body>
        <p>
          <xsl:call-template name="sumScores">
            <xsl:with-param name="pList" select="/accounts/account/score"/>
          </xsl:call-template>
        </p>
      </body>
    </html>
  </xsl:template>

  <xsl:template name="sumScores">
    <xsl:param name="pList"/>
    <xsl:param name="pAccum" select="0"/>
    <xsl:choose>
      <xsl:when test="$pList">
        <xsl:variable name="vHead" select="$pList[1]"/>
        <xsl:call-template name="sumScores">
          <xsl:with-param name="pList" select="$pList[position() > 1]"/>
          <xsl:with-param name="pAccum"
                          select="$pAccum + number(substring-before($vHead,'/'))"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$pAccum"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>


(信用:来源于。)

当使用输入XML运行XSLT 1.0转换时,我们会得到所需的HTML输出:

<html>
   <body>
      <p>180</p>
   </body>
</html>

180


看来@kjhughes不知怎么搞清楚了,您使用的是XSLT 1.0处理器,而您是在web浏览器中运行的


您需要一个XSLT2.0处理器来运行此操作。如果您确实在Web浏览器中运行,请考虑SAXON CE,这是目前唯一运行客户端的2个处理器。

您看到了什么错误?Safari和Firefox不工作。在Oxygen编辑器中使用XPATH是可行的,所以我不知道为什么在这个浏览器中不起作用!我需要它们在此浏览器中工作。它告诉我要计算的表达式无效。浏览器通常不支持XSLT 2.0,而且很可能永远不会支持。在服务器上运行模板。XSLT不适用于客户端。我很困惑,因为如果我有一个标记号而不是分数(例如:5)并执行“”,它就会工作。。为什么这行不通?@SteveAwsd这是因为在XPath 1.0中,简化的大小写在最后一个
/
之后没有
number()
函数是可以的。您需要XPath2.0来支持最终
/
之后的函数。
<html>
   <body>
      <p>180</p>
   </body>
</html>