3

与这个问题(http://stackoverflow.com/q/1333558/948404)类似,我想使用 XPath 计算结构中的产品总和,如下所示:

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

是否有一个 XPath 表达式计算每个项目的乘积之valuequantity

更新:该解决方案必须与 PHP 的 XSLTProcessor 类一起使用,这意味着它可能必须符合 XSLT 1.0。这就是为什么我还没有接受使用 XSLT 2.0 的两个可能正确的答案。我无法在我的 PHP 实现、浏览器或 w3schools 的 Tryit Editor [1] 中测试它们。对不起!

  1. http://w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog
4

3 回答 3

4

使用这个 XPath 2.0 表达式

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

Here is an XSLT 2.0 transformation as verification:

<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>

When this transformation is applied on the provided XML document:

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

the XPath expression is evaluated and the result of this evaluation is output:

8

Explanation:

In XPath 2.0 it is legal for a location step to be

/(expression),

or even

/someFunction(argumentList)


II. XSLT 1.0 solution:

<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>

when this transformation is applied on the provided XML document (above), again the wanted, correct result is produced:

8

Do note: Such kind of problems are easy to solve using the FXSL library. The template to call is transform-and-sum .

于 2012-05-20T02:32:04.120 回答
1

试试这个:

<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>
于 2012-05-19T23:03:06.840 回答
1

Based on this suggestion https://stackoverflow.com/a/1334165/948404 I came up with a solution compliant with XSLT 1.0 and XPath 1.0, which are implemented by libxml/libxslt, used by PHPs implementation of XSLTProcessor (thanks to @Dimitre Novatchev for 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>
于 2012-05-21T13:32:13.557 回答