问题描述
我有一个父变量和两个子变量进行累加
I have a parent variable and two child variables for accumulate
我想用这两个子变量来选择两个不同的值
I would like to use those two child variables to select two different values
我只能对其进行硬编码以使其像所有其他教程
一样工作:$ parent [1] / Price和$ parent [1] / Quantity
I can only hardcode it to make it works like all other tutorialfor example: $parent[1]/Price and $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>
推荐答案
目前尚不清楚问题是什么,但是我的猜测是这样。
It is not exactly clear what the question is, but my guess is this.
这种转变:
<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文档(您可以至少提供此!):
when applied on the following XML document (you could at least provide this!):
<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
请注意:用作价格和数量的子元素名称的值作为转换的外部参数提供,并且只能在运行时知道。
Do note: The values of the names of the child elements to serve as price and quantity are provided as external parameters to the transformation and can be known only at run-time.
这篇关于XSLT累积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!