【问题标题】:XSLT accumulateXSLT 累积
【发布时间】:2011-05-30 23:16:12
【问题描述】:

我有一个父变量和两个子变量用于累积

我想用这两个子变量来选择两个不同的值

我只能对其进行硬编码以使其像所有其他教程一样工作 例如:$parent[1]/Price 和 $parent[1]/Quantity

但我想要以下内容:

$parent[1]/$child1 where $parent[1] = orders[1]/order and $child1 = price
$parent[1]/$child2 where $parent[1] = orders[1]/order and $child2 = quantity

    <xsl:call-template name="total">
    <xsl:with-param name="pList" select="$parent"/>
    <xsl:with-param name="price" select="Price"/>
    <xsl:with-param name="quantity" select="Quantity"/>   
    </xsl:call-template>

<xsl:template name="total">
    <xsl:param name="pListItem"/>
    <xsl:param name="pAccum" select="0"/>
    <xsl:param name="price"/>
    <xsl:param name="quantity"/>

    <xsl:choose>
      <xsl:when test="$pListItem">
        <xsl:call-template name="total">
          <xsl:with-param name="pListItem" select="$pListItem[position() > 1]"/>
          <xsl:with-param name="pAccum"
           select="$pAccum + $vHead/$price * $vHead/$quantity"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$pAccum"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

【问题讨论】:

  • 虽然不清楚你的问题是什么(根本没有指定问题),但我尝试尽可能多地猜测,并在我的回答中提供了一个完整、简短、通用且简单的解决方案我相信是你的问题。 +1 艰难的开始。一开始一切都很困难:)

标签: xml xslt accumulate


【解决方案1】:

目前还不清楚问题是什么,但我的猜测是这样的。

这种转变

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

 <xsl:param name="pPriceName" select="'price2'"/>
 <xsl:param name="pQuantityName" select="'quantity1'"/>


 <xsl:template match="/">
  <xsl:call-template name="totalSales">
   <xsl:with-param name="pSales" select="/*/*"/>
   <xsl:with-param name="pPriceName" select="$pPriceName"/>
   <xsl:with-param name="pQuantityName"
        select="$pQuantityName"/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template name="totalSales">
  <xsl:param name="pAccum" select="0"/>
  <xsl:param name="pSales"/>
  <xsl:param name="pPriceName"/>
  <xsl:param name="pQuantityName"/>

  <xsl:variable name="vthisSale" select="$pSales[1]"/>

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

应用于以下 XML 文档时(您至少可以提供这个!):

<orders>
 <order>
  <price1>10</price1>
  <quantity1>3</quantity1>
  <price2>15</price2>
  <quantity2>1</quantity2>
 </order>
 <order>
  <price1>11</price1>
  <quantity1>2</quantity1>
  <price2>9</price2>
  <quantity2>3</quantity2>
 </order>
</orders>

产生想要的正确结果:

63

注意:作为价格和数量的子元素名称的值作为外部参数提供给转换,并且只能在运行时知道。

【讨论】:

    【解决方案2】:

    非常感谢。
    我只需要'Price'和$vthisSale/*[name()=$pPriceName]

    真的很有帮助!!我被魔法卡住了好久''

    【讨论】:

    • @Franky:这次和下一次,请考虑更好地参与 SO 的工作原理。请务必阅读有关如何使用 accept answersupvote responsibly 的信息。
    猜你喜欢
    • 2011-10-24
    • 2018-05-13
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    • 2013-11-08
    • 2019-05-04
    相关资源
    最近更新 更多