Aggregate function for sum and product in XPath

Like this question (http://stackoverflow.com/q/1333558/948404) I want to use XPath to calculate the sum over products in a structure like this:

<items>
    <item>
        <value>1.0</value>
        <quantity>3</quantity>
    </item>
    <item>
        <value>2.5</value>
        <quantity>2</quantity>
    </item>
    <!-- ... -->
</items>

Is there an XPath expression that calculates the sum of the products of each element valueand quantity?

Update: The solution should work with the XSLTP class of the PHP processor, which means that it probably should comply with XSLT 1.0 requirements. This is why I have not yet accepted two probably correct answers using XSLT 2.0. I could not test them in my PHP implementation, neither in my browser, nor in Tryit Editor [1] from w3schools. Excuse me!

+3
source
3

XPath 2.0:

sum(/items/item/(value * quantity))

XSLT 2.0 :

<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(/items/item/(value * quantity))"/>
    </xsl:template>
</xsl:stylesheet>

XML-:

<items>
    <item>
        <value>1.0</value>
        <quantity>3</quantity>
    </item>
    <item>
        <value>2.5</value>
        <quantity>2</quantity>
    </item>
    <!-- ... -->
</items>

XPath :

8

XPath 2.0 ,

/(expression),

/someFunction(argumentList)


II. XSLT 1.0:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

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

 <xsl:template name="sumProducts">
   <xsl:param name="pNodes" select="/.."/>
   <xsl:param name="pAccum" select="0"/>

   <xsl:choose>
    <xsl:when test="not($pNodes)">
     <xsl:value-of select="$pAccum"/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:call-template name="sumProducts">
      <xsl:with-param name="pNodes" select="$pNodes[position() >1]"/>
      <xsl:with-param name="pAccum" select=
      "$pAccum + $pNodes[1]/value * $pNodes[1]/quantity"/>
     </xsl:call-template>
    </xsl:otherwise>
   </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

, XML- (. ), , :

8

. FXSL > . transform-and-sum.

+4

:

<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(/items/item/(value * quantity))"/>
    </xsl:template>
</xsl:stylesheet>
+1

Based on this suggestion fooobar.com/questions/857426 / ... , I came up with a solution that matches XSLT 1.0 and XPath 1.0, which are implemented by libxml / libxslt used by the PHP implementation XSLTProcessor(thanks @Dimitre Novatchev for the confirmation).

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

    <xsl:template name="recursivesum">
        <xsl:param name="items" />
        <xsl:param name="sum" select="0" />
        <xsl:variable name="head" select="$items[1]" />
        <xsl:variable name="tail" select="$items[position()>1]" />
        <xsl:variable name="thissum" select="$head/value * $head/quantity" />
        <xsl:choose>
            <xsl:when test="not($tail)">
                <xsl:value-of select="$sum+$thissum" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="recursivesum">
                    <xsl:with-param name="sum" select="$sum+$thissum" />
                    <xsl:with-param name="items" select="$tail" />
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:transform>
+1
source

All Articles