English 中文(简体)
XPath的合计和产品综合功能
原标题:Aggregate-function for sum and product in XPath

类似这个问题(http://stackoverflow.com/q/1333558/9404) 我想利用XPath,在类似结构中计算出一笔高于产品的款项:

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

是否有计算每个项目产品总和的XPath表述?

Update: The solution has to work with PHPs XSLTProcessor class meaning that it probably has to be XSLT 1.0 compliant. This is why I did not yet accept the two probably correct answers using XSLT 2.0. I could neither test them in my PHP implementation nor in my browser nor in the Tryit Editor [1] from w3schools. Sorry!

  1. http://w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog
最佳回答

www.un.org/spanish/ecosoc 采用2.0代语:

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

www.un.org/spanish/ecosoc 这里的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>

www.un.org/spanish/ecosoc 当这一转变适用于所提供的XML文件:。

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

XPath expression is evaluation and the results:

8

<>Explanation:

在XPath 2.0中,地点步骤是合法的

http://www.un.org/Depts/DGACM/index_spanish.htm

or even


II.XSLT 1.0 Solutions:

<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

transform-and-sum。

问题回答

引证:

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




相关问题
XSLT Attribute not being added

Trying to mark radio inputs as selected with XSLT 1.0 using the below xslt code but this does not produced the desired result desrired result <input type="radio" value="available" title="email" ...

XSLT 1.0 How to extend with fn (function namespace)

I was wondering how I can possible extend XSLT 1.0 so that I can use functions from fn function namespace at http://www.w3schools.com/Xpath/xpath_functions.asp I was just told that the system is ...

Performing a "Group By" query in XPath XSL

Given the following XML: <results name="queryResults"> <int name="intfield1:[* TO 10]">11</int> <int name="intfield2:[10 TO 20]">9</int> <int name="intfield1:[...

XSLT:How to deal with testing the value of an element?

I have an xml file in which there is tag namely, <Gender/> It carries either M or F as data, now my work is to test the value and write <Gender_Tag>Male</Gender_Tag> or <...

When test hanging in an infinite loop

I m tokenising a string with XSLT 1.0 and trying to prevent empty strings from being recognised as tokens. Here s the entire function, based on XSLT Cookbook: <xsl:template name="tokenize"> ...

热门标签