乘以2个数字然后求和

时间:2009-01-12 21:04:35

标签: xslt

我正在努力做一些似乎应该很容易做的事情。我基本上想要在一个节点中乘以2个数字,然后将所有节点的总数加起来。这是我尝试过的XSLT代码。

<xsl:value-of select="sum(Parts/Part/Quantity * Parts/Part/Rate)"/>

此代码导致出现“函数和的参数1无法转换为节点集”的错误。

有没有人知道出了什么问题,或者我怎么能完成我想做的事情?

2 个答案:

答案 0 :(得分:37)

以下是三种可能的解决方案

Solution1 XSLT2:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

    <xsl:template match="/">
      <xsl:sequence select="sum(/*/*/(rate * quantity))"/>
    </xsl:template>
</xsl:stylesheet>

将此转换应用于以下XML文档

<parts>
  <part>
        <rate>0.37</rate>
    <quantity>10</quantity>
  </part>
  <part>
        <rate>0.03</rate>
    <quantity>10</quantity>
  </part>
</parts>

生成了想要的结果

4

XSLT 2.0解决方案使用以下事实:在XPath 2.0中允许最后一个“/”运算符的右参数可以是表达式或通常是函数。此表达式/函数适用于目前为止作为上下文节点选择的每个节点,每个函数应用程序都会生成一个结果。

Solution2 XSLT 1.0:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

    <xsl:template match="/">
      <xsl:call-template name="sumProducts">
        <xsl:with-param name="pList" select="*/*"/>
      </xsl:call-template>
    </xsl:template>

    <xsl:template name="sumProducts">
        <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="sumProducts">
              <xsl:with-param name="pList" select="$pList[position() > 1]"/>
              <xsl:with-param name="pAccum"
               select="$pAccum + $vHead/rate * $vHead/quantity"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$pAccum"/>
          </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

应用于上述XML文档时,会生成正确的结果

4

这是典型的XSLT 1.0递归解决方案请注意sumProducts模板如何递归调用,直到处理完参数$pList中的整个输入列表。

Solution3 FXSL(XSLT 1.0):

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
xmlns:test-map-product="test-map-product"
exclude-result-prefixes="xsl ext test-map-product"
>
   <xsl:import href="sum.xsl"/>
   <xsl:import href="map.xsl"/>
   <xsl:import href="product.xsl"/>

   <!-- This transformation is to be applied on:
        salesMap.xml

        It contains the code of the "sum of products" from the 
        article "The Functional Programming Language XSLT"
     -->

   <test-map-product:test-map-product/>

   <xsl:output method="text"/>

   <xsl:template match="/">
     <!-- Get: map product /sales/sale -->
     <xsl:variable name="vSalesTotals">
         <xsl:variable name="vTestMap" select="document('')/*/test-map-product:*[1]"/>
         <xsl:call-template name="map">
           <xsl:with-param name="pFun" select="$vTestMap"/>
           <xsl:with-param name="pList1" select="/sales/sale"/>
         </xsl:call-template>
     </xsl:variable>

     <!-- Get sum map product /sales/sale -->
      <xsl:call-template name="sum">
        <xsl:with-param name="pList" select="ext:node-set($vSalesTotals)/*"/>
      </xsl:call-template>
   </xsl:template>

    <xsl:template name="makeproduct" match="*[namespace-uri() = 'test-map-product']">
      <xsl:param name="arg1"/>

      <xsl:call-template name="product">
        <xsl:with-param name="pList" select="$arg1/*"/>
      </xsl:call-template>
    </xsl:template>
</xsl:stylesheet>

将此转换应用于以下XML文档

<sales>
  <sale>
    <price>3.5</price>
    <quantity>2</quantity>
    <Discount>0.75</Discount>
    <Discount>0.80</Discount>
    <Discount>0.90</Discount>
  </sale>
  <sale>
    <price>3.5</price>
    <quantity>2</quantity>
    <Discount>0.75</Discount>
    <Discount>0.80</Discount>
    <Discount>0.90</Discount>
  </sale>
</sales>

产生了正确的结果

7.5600000000000005

在每个sale的最后一种情况下,我们计算pricequantity和所有可用(可变数量)discount - s的乘积。

FXSL 是高阶函数的纯XSLT实现。在此示例中,使用高阶函数f:map() 将函数f:product()映射到每个sale元素的子元素列表中。然后将结果相加以产生最终结果。

答案 1 :(得分:1)

所有Dimitre的解决方案都有效,他说你没有需要来使用扩展功能,但有时它会让生活更轻松。它并没有太大的危害,特别是当您使用多个XSLT处理器支持的exslt扩展时。此外,您获得序列错误的原因可能是因为您使用的是XSLT 1处理器。

如果您想坚持使用所选择的解决方案,则需要使用Saxon或其他支持XSLT 2的XSLT处理器。

否则,这是在XSLT 1中执行此操作的另一种方法。这将适用于大多数XSLT处理器,并且某些peope可能会发现它比递归版本更容易理解。就个人而言,我更喜欢递归版(Dimitre的第三个提案),因为它更便携。

<xsl:stylesheet version="1.0"
                xmlns:ex="http://exslt.org/common"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>

  <xsl:template name="GetProducts">
    <xsl:param name="left"/>
    <xsl:param name="right"/>

    <xsl:for-each select="$left/text()">
      <product>
        <xsl:value-of select="number(.) * number($right[position()])"/>
      </product>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="/">
    <xsl:variable name="products">
      <xsl:call-template name="GetProducts">
        <xsl:with-param name="left" select="Parts/Part/Rate"/>
        <xsl:with-param name="right" select="Parts/Part/Quantity"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:value-of select="sum(ex:node-set($products)/product)"/>
  </xsl:template>
</xsl:stylesheet>