[英]XSLT 1.0 : sum of math calculations of sub-elements
我需要使用子元素中的數據計算包裹中物品的總價值(總和)。 我試圖通過使用 XSLT(僅 1.0 版可用)進行一些計算來解決源系統中的問題。 問題看起來像這樣:
<Shipment>
...
<Parcels>
<Parcel>
<Lenght>10</Lenght>
<Width>20</Width>
<Height>30</Height>
<Weight>10</Weight>
<Products>
<Product>
<ItemNo>12345</ItemNo>
<Description>Shirt</Description>
<OrderedQty>4</OrderedQty>
<DeliveredQty>2</DeliveredQty>
<LineValue>400</LineValue>
<Currency>EUR</Currency>
</Product>
<Product>
<ItemNo>54321</ItemNo>
<Description>Trousers</Description>
<OrderedQty>1</OrderedQty>
<DeliveredQty>1</DeliveredQty>
<LineValue>50</LineValue>
<Currency>EUR</Currency>
</Product>
</Products>
</Parcel>
<Parcel>
<Lenght>15</Lenght>
<Width>25</Width>
<Height>35</Height>
<Weight>5</Weight>
<Products>
<Product>
<ItemNo>23456</ItemNo>
<Description>Jacket</Description>
<OrderedQty>2</OrderedQty>
<DeliveredQty>1</DeliveredQty>
<LineValue>300</LineValue>
<Currency>EUR</Currency>
</Product>
</Products>
</Parcel>
</Parcels>
</Shipment>
當有部分交付時,為每個包裹生成項目信息的系統將錯誤地計算 LineValue。 在上面的示例 XML 中,第一個包裹中的商品 12345(襯衫)的 UnitValue 為 100,系統生成的 LineValue 為 400 (OrderedQty * UnitValue)。 但是,只交付了 2/4 的項目,因此正確的 LineValue 應該是 DeliveredQty * 100 = 200。由於我沒有 XML 中的實際單位值,因此需要在每個 Product - 元素中進行一些計算。 我可以用一些變量來做到這一點:
<xsl:for-each select="Products/Product">
<xsl:variable name="OrderedQty">
<xsl:value-of select="OrderedQty"/>
</xsl:variable>
<xsl:variable name="DeliveredQty">
<xsl:value-of select="DeliveredQty"/>
</xsl:variable>
<xsl:variable name="LineValue">
<xsl:value-of select="LineValue"/>
</xsl:variable>
<xsl:variable name="UnitValue">
<xsl:value-of select="$LineValue div $OrderQty"/>
</xsl:variable>
<xsl:variable name="RealLineValue">
<xsl:value-of select="$DeliveredQty * $UnitValue"/>
</xsl:variable>
</xsl:for-each>
所以我得到了每個產品的正確單位值和新計算的行值 OK...但是...(真正的挑戰來了):
我如何通過將每個產品元素中計算出的 $RealLineValues 相加來對 Shipment 級別進行總和計算?
我嘗試使用從 StackOverflow 找到的遞歸模板:
<xsl:template name="sum">
<xsl:param name="nodes"/>
<xsl:param name="sum" select="0"/>
<xsl:variable name="current" select="$nodes[1]"/>
<xsl:if test="$current">
<xsl:call-template name="sum">
<xsl:with-param name="nodes" select="$nodes[position() > 1]"/>
<xsl:with-param name="sum" select="$sum + $current/$RealLineValue"/>
</xsl:call-template>
</xsl:if>
<xsl:if test="not($current)">
<xsl:value-of select="$sum"/>
</xsl:if>
</xsl:template>
並且通過從上層調用該模板,但顯然變量 $RealLineValue 不能那樣使用。
正如您從我的試驗中看到的那樣,我不太擅長 XSLT,所以非常感謝任何幫助 ID:)
如果您可以提供解決方案,使用 XSLT 1.0 計算每個包裹中每個產品的“總和(LineValue div OrderedQty)* DeliveredQty)”到裝運級別,那就太棒了!
是的,結果應該是 2*(400/4) + 1(50/1) + 1(300/2) = 200 + 50 + 150 = 400
非常感謝提前。
如果你想使用遞歸模板,你可以這樣做:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Shipment">
<sum>
<xsl:call-template name="sum">
<xsl:with-param name="nodes" select="//Product"/>
</xsl:call-template>
</sum>
</xsl:template>
<xsl:template name="sum">
<xsl:param name="nodes"/>
<xsl:param name="sum" select="0"/>
<xsl:variable name="current" select="$nodes[1]"/>
<xsl:choose>
<xsl:when test="$current">
<xsl:variable name="UnitValue" select="$current/LineValue div $current/OrderedQty"/>
<xsl:variable name="RealLineValue" select="$current/DeliveredQty * $UnitValue"/>
<xsl:call-template name="sum">
<xsl:with-param name="nodes" select="$nodes[position() > 1]"/>
<xsl:with-param name="sum" select="$sum + $RealLineValue"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$sum"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
有關替代方法,請參閱: XSL 如何計算每個元素的屬性值的乘積之和
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.