XSLT总结两个属性的产品(XSLT to sum product of two attribute

2019-07-03 17:53发布

我有以下XML源结构:

<turnovers>
    <turnover repid="1" amount="500" rate="0.1"/>
    <turnover repid="5" amount="600" rate="0.5"/>
    <turnover repid="4" amount="400" rate="0.2"/>
    <turnover repid="1" amount="700" rate="0.05"/>
    <turnover repid="2" amount="100" rate="0.15"/>
    <turnover repid="1" amount="900" rate="0.25"/>
    <turnover repid="2" amount="1000" rate="0.18"/>
    <turnover repid="5" amount="200" rate="0.55"/>
    <turnover repid="9" amount="700" rate="0.40"/>
</turnovers>

我需要一个XSL:select语句价值的,将返回速率属性和金额属性的乘积的总和对于一个给定代表ID。 因此,对于代表5我需要((600×0.5)+(200×0.55))。

Answer 1:

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:template match="/turnovers">
    <val>
      <!-- call the sum function (with the relevant nodes) -->
      <xsl:call-template name="sum">
        <xsl:with-param name="nodes" select="turnover[@repid='5']" />
      </xsl:call-template>
    </val>
  </xsl:template>

  <xsl:template name="sum">  
    <xsl:param name="nodes" />
    <xsl:param name="sum" select="0" />

    <xsl:variable name="curr" select="$nodes[1]" />

    <!-- if we have a node, calculate & recurse -->
    <xsl:if test="$curr">
      <xsl:variable name="runningsum" select="
        $sum + $curr/@amount * $curr/@rate
      " />
      <xsl:call-template name="sum">
        <xsl:with-param name="nodes" select="$nodes[position() &gt; 1]" />
        <xsl:with-param name="sum"   select="$runningsum" />
      </xsl:call-template>
    </xsl:if>

    <!-- if we don't have a node (last recursive step), return sum -->
    <xsl:if test="not($curr)">
      <xsl:value-of select="$sum" />
    </xsl:if>

  </xsl:template>
</xsl:stylesheet>

得到:

<val>410</val>

两个<xsl:if> S可被替换为一个单一<xsl:choose> 。 这意味着在递归过程中少了一个检查,但它也意味着代码两条额外的线条。



Answer 2:

在普通的XSLT 1.0,你需要为这个递归模板,例如:

  <xsl:template match="turnovers">
    <xsl:variable name="selectedId" select="5" />
    <xsl:call-template name="sum_turnover">
      <xsl:with-param name="turnovers" select="turnover[@repid=$selectedId]" />
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="sum_turnover">
    <xsl:param name="total" select="0" />
    <xsl:param name="turnovers"  />
    <xsl:variable name="head" select="$turnovers[1]" />
    <xsl:variable name="tail" select="$turnovers[position()>1]" />
    <xsl:variable name="calc" select="$head/@amount * $head/@rate" />
    <xsl:choose>
      <xsl:when test="not($tail)">
        <xsl:value-of select="$total + $calc" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="sum_turnover">
          <xsl:with-param name="total" select="$total + $calc" />
          <xsl:with-param name="turnovers" select="$tail" />
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


Answer 3:

这应该做的伎俩,你需要做一些进一步的工作来选择不同的repid

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

    <xsl:template match="/">    
        <xsl:variable name="totals">
            <product>
                <xsl:for-each select="turnovers/turnover">
                    <repid repid="{@repid}">
                        <value><xsl:value-of select="@amount * @rate"/></value>
                    </repid>
                </xsl:for-each>
            </product>
        </xsl:variable>
        <totals>
            <total repid="5" value="{sum($totals/product/repid[@repid='5']/value)}"/>   
        </totals>               
    </xsl:template>

</xsl:stylesheet>


Answer 4:

在XSLT 1.0使用FXSL使得容易解决这样的问题:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:f="http://fxsl.sf.net/"
 xmlns:ext="http://exslt.org/common"
 exclude-result-prefixes="xsl f ext"
 >
 <xsl:import href="zipWith.xsl"/>
 <xsl:output method="text"/>

  <xsl:variable name="vMultFun" select="document('')/*/f:mult-func[1]"/>    

    <xsl:template match="/"> 
      <xsl:call-template name="profitForId"/>
    </xsl:template>

    <xsl:template name="profitForId">
      <xsl:param name="pId" select="1"/>

      <xsl:variable name="vrtfProducts">
          <xsl:call-template name="zipWith">
            <xsl:with-param name="pFun" select="$vMultFun"/>
            <xsl:with-param name="pList1" select="/*/*[@repid = $pId]/@amount"/>
            <xsl:with-param name="pList2" select="/*/*[@repid = $pId]/@rate"/>
          </xsl:call-template>
      </xsl:variable>

      <xsl:value-of select="sum(ext:node-set($vrtfProducts)/*)"/>
    </xsl:template>

    <f:mult-func/>
    <xsl:template match="f:mult-func" mode="f:FXSL">
     <xsl:param name="pArg1"/>
     <xsl:param name="pArg2"/>

     <xsl:value-of select="$pArg1 * $pArg2"/>
    </xsl:template>
</xsl:stylesheet>

当在最初发布源XML文档应用这种转变,正确的结果产生:

310

在XSLT 2.0使用同一溶液FXSL 2.0可以由XPath单行来表示:

sum(f:zipWith(f:multiply(),
          /*/*[xs:decimal(@repid) eq 1]/@amount/xs:decimal(.),
          /*/*[xs:decimal(@repid) eq 1]/@rate/xs:decimal(.)
          )
     )

整个改造:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:f="http://fxsl.sf.net/"
 exclude-result-prefixes="f xs"
>
 <xsl:import href="../f/func-zipWithDVC.xsl"/>
 <xsl:import href="../f/func-Operators.xsl"/>

 <!-- To be applied on testFunc-zipWith4.xml -->
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
   <xsl:value-of select=
   "sum(f:zipWith(f:multiply(),
              /*/*[xs:decimal(@repid) eq 1]/@amount/xs:decimal(.),
              /*/*[xs:decimal(@repid) eq 1]/@rate/xs:decimal(.)
              )
         )
    "/>
 </xsl:template>
</xsl:stylesheet>

同样,这种转变产生正确的答案:

310

请注意以下几点

  1. f:zipWith()函数采用作为自变量的函数fun()的两个参数),并具有相同长度的物品的两个列表。 它产生的相同的长度,其项目是成对应用的结果的一个新的列表fun()上的相应k两个列表的第项目。

  2. f:zipWith()如表达式取函数f:multiply()和相应的“的两个序列ammount ”和“ rate ”属性。 所述sesult是一个序列,每一个项目,其中是对应“的产品ammount ”和“ rate ”。

  3. 最后, 总和该序列的产生。

  4. 有没有必要写一个明确的递归和它也保证了内使用的幕后场景递归f:zipWith()是永远不会崩溃(适用于所有实际情况)与“堆栈溢出”



Answer 5:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="xs"
      version="2.0">

   <xsl:variable name="repid" select="5" />

   <xsl:template match="/">
      <xsl:value-of select=
         "sum(for $x in /turnovers/turnover[@repid=$repid] return $x/@amount * $x/@rate)"/>
   </xsl:template>

</xsl:stylesheet>

你可以做到这一点,如果你只需要值,而不是XML。



Answer 6:

做到这一点在XSLT最简单的方法可能是使用的编程语言绑定,这样就可以定义自己的XPath函数。



文章来源: XSLT to sum product of two attributes
标签: xslt xpath sum