使用XSLT 2.0进行数据的汇总变换。

huangapple go评论50阅读模式
英文:

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:templatexsl: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

&lt;xsl:stylesheet version=&quot;2.0&quot; 
xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
&lt;xsl:output method=&quot;xml&quot; version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; indent=&quot;yes&quot;/&gt;

&lt;xsl:param name=&quot;priceList&quot;&gt;
	&lt;food type=&quot;apple&quot;&gt;1&lt;/food&gt;
	&lt;food type=&quot;orange&quot;&gt;2&lt;/food&gt;
&lt;/xsl:param&gt;

&lt;xsl:key name=&quot;price&quot; match=&quot;food&quot; use=&quot;@type&quot; /&gt;
	
&lt;xsl:template match=&quot;/cart&quot;&gt;
	&lt;xsl:variable name=&quot;boxes&quot;&gt;
		&lt;xsl:for-each select=&quot;box&quot;&gt;
			&lt;box value=&quot;{sum(food/(@quantity * key(&#39;price&#39;, @type, $priceList)))}&quot;/&gt;
		&lt;/xsl:for-each&gt;
	&lt;/xsl:variable&gt;
	&lt;result&gt;
		&lt;xsl:copy-of select=&quot;$boxes&quot;/&gt;
		&lt;sum&gt;
			&lt;xsl:value-of select=&quot;sum($boxes/box/@value)&quot;/&gt;
		&lt;/sum&gt;
		&lt;max&gt;
			&lt;xsl:value-of select=&quot;max($boxes/box/@value)&quot;/&gt;
		&lt;/max&gt;
	&lt;/result&gt;
&lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;

Applied to your XML example, this will return:

Result

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;result&gt;
   &lt;box value=&quot;7&quot;/&gt;
   &lt;box value=&quot;8&quot;/&gt;
   &lt;sum&gt;15&lt;/sum&gt;
   &lt;max&gt;8&lt;/max&gt;
&lt;/result&gt;

huangapple
  • 本文由 发表于 2023年7月17日 23:14:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定