英文:
Summing transformative data using XSLT 2.0
问题
以下是要翻译的内容:
假设我有这个示例的XML:
<cart>
<box>
<food type="apple" quantity="3"/>
<food type="orange" quantity="2"/>
</box>
<box>
<food type="apple" quantity="6"/>
<food type="orange" quantity="1"/>
</box>
</cart>
如果苹果价格为$1,橙子价格为$2,我想要将购物车的总价值存储到一个XSLT变量中(在这种情况下,总价值将为$7 + $8 = $15)。
<xsl:variable name=cartsum>
//在此处迭代购物车中所有箱子中的食物,将它们转换为美元,并将它们相加
</xsl:variable>
我知道可以使用xsl:template
和xsl:choose
来获得类似于字符串"((3 * 1) + (2 * 2)) + ((6 * 1) + (1 * 2))",但如何将这个总和存储到一个变量中,以便在其他xsl:if
评估中使用呢?
此外,还有一个相关的问题,我想要将最贵的箱子(即箱子值的最大值)分配给另一个变量,本例中为$8。希望这两个解决方案(总和和最大值)可以以相同的方式实现。
顺便说一下,我正在使用Saxon-HE 9.6.0.4N作为我的XSLT引擎。
英文:
Say I have this example XML:
<cart>
<box>
<food type="apple" quantity="3"/>
<food type="orange" quantity="2"/>
</box>
<box>
<food type="apple" quantity="6"/>
<food type="orange" quantity="1"/>
</box>
</cart>
If apples cost $1 and oranges cost $2, I would like to get the value of the sum of the cart into an XSLT variable (in the case the sum would be $7 + $8 = $15).
<xsl:variable name=cartsum>
//Something in here to iterate over the food in all the boxes
//in the cart, transform them to dollars, and add them together
</xsl:variable>`
I know I can use xsl:template
's and use xsl:choose
's to get something like the string "((3 * 1) + (2 * 2)) + ((6 * 1) + (1 * 2))", but how do I get that sum into a variable that I can use in other xsl:if
evaluations?
Also, another related question, I would like to assign to a different variable the most expensive box (i.e. the max of the box values) as well, which in this case it would be $8. Hopefully both solutions (sum and max) can be achieved the same way.
BTW, I am using Saxon-HE 9.6.0.4N as my XSLT engine
答案1
得分: 0
考虑以下示例:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:param name="priceList">
<food type="apple">1</food>
<food type="orange">2</food>
</xsl:param>
<xsl:key name="price" match="food" use="@type" />
<xsl:template match="/cart">
<xsl:variable name="boxes">
<xsl:for-each select="box">
<box value="{sum(food/(@quantity * key('price', @type, $priceList)))}"/>
</xsl:for-each>
</xsl:variable>
<result>
<xsl:copy-of select="$boxes"/>
<sum>
<xsl:value-of select="sum($boxes/box/@value)"/>
</sum>
<max>
<xsl:value-of select="max($boxes/box/@value)"/>
</max>
</result>
</xsl:template>
</xsl:stylesheet>
应用于您的XML示例,这将返回:
Result
<?xml version="1.0" encoding="UTF-8"?>
<result>
<box value="7"/>
<box value="8"/>
<sum>15</sum>
<max>8</max>
</result>
英文:
Consider the following example:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:param name="priceList">
<food type="apple">1</food>
<food type="orange">2</food>
</xsl:param>
<xsl:key name="price" match="food" use="@type" />
<xsl:template match="/cart">
<xsl:variable name="boxes">
<xsl:for-each select="box">
<box value="{sum(food/(@quantity * key('price', @type, $priceList)))}"/>
</xsl:for-each>
</xsl:variable>
<result>
<xsl:copy-of select="$boxes"/>
<sum>
<xsl:value-of select="sum($boxes/box/@value)"/>
</sum>
<max>
<xsl:value-of select="max($boxes/box/@value)"/>
</max>
</result>
</xsl:template>
</xsl:stylesheet>
Applied to your XML example, this will return:
Result
<?xml version="1.0" encoding="UTF-8"?>
<result>
<box value="7"/>
<box value="8"/>
<sum>15</sum>
<max>8</max>
</result>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论