XSL Multiplying a value times a value in an value of select
XSL Multiplying a value times a value in an value of select
I have this piece of code:
<xsl:value-of
select="sum(objects/object/items/item[not(product_type='configurable' and
count(custom_options/custom_option[option_id='2686']) >
0)]/custom_options/custom_option[option_id='2686']/value)" />
Which correctly outputs the integer that I want, but now in a different field, I want to mutiply this by the value of, in my case, qty_ordered
. So what I tried is this:
qty_ordered
<xsl:value-of
select="sum(objects/object/items/item[not(product_type='configurable' and
count(custom_options/custom_option[option_id='2686']) >
0)]/custom_options/custom_option[option_id='2686']/value * qty_ordered)" />
Unfortunately this doesn't work and I have no idea how to do this, anyone able to help me produce a line of code which outputs id2686/value * qty_ordered
? qty_ordered
is also in the items/item loop.
id2686/value * qty_ordered
qty_ordered
This is the input:
<items>
<item>
<qty_ordered>5.0000</qty_ordered>
and
<custom_option>
<name>Aantal pagina's in PDF</name>
<value>1</value>
<sku/>
<option_id>2686</option_id>
</custom_option>
Unfortunately I can't use XSLT 2.0, and thank you for your tip, i'll do this right away!
– RsDob
Jun 29 at 9:00
1 Answer
1
In XSLT 1.0, there a couple of ways to do this. One way is to create a temporary result tree which holds the individual values of qty_ordered
and value
for each item, like so:
qty_ordered
value
<xsl:variable name="items">
<xsl:for-each select="item[not(product_type='configurable')]">
<item>
<xsl:value-of select="qty_ordered * custom_options/custom_option/value" />
</item>
</xsl:for-each>
</xsl:variable>
However, in XSLT 1.0, you will need to make use of an extension function to access the nodes in the variable (as the items
variable is of type "result tree fragment" and needs to be converted into a node set for use with the sum function). The most common one to use is exslt.
items
Try this XSLT (I've simplified the xpath expression for brevity)
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exslt="http://exslt.org/common"
exclude-result-prefixes="exslt"
version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="items">
<xsl:variable name="items">
<xsl:for-each select="item[not(product_type='configurable')]">
<item>
<xsl:value-of select="qty_ordered * custom_options/custom_option/value" />
</item>
</xsl:for-each>
</xsl:variable>
<result>
<xsl:value-of select="sum(exslt:node-set($items)/item)" />
</result>
</xsl:template>
</xsl:stylesheet>
When you apply it to this XML
<items>
<item>
<qty_ordered>5.0000</qty_ordered>
<custom_options>
<custom_option>
<name>Aantal pagina's in PDF</name>
<value>1</value>
<sku/>
<option_id>2686</option_id>
</custom_option>
</custom_options>
</item>
<item>
<qty_ordered>2.0000</qty_ordered>
<custom_options>
<custom_option>
<name>Aantal pagina's in PDF - Vol 2</name>
<value>3</value>
<sku/>
<option_id>2687</option_id>
</custom_option>
</custom_options>
</item>
</items>
The result is this...
<result>11</result>
EDIT: It's a bit difficult to explain how to integrate this in with your existing code because you haven't shown enough of your existing XSLT
But where you currently do this...
<xsl:value-of
select="sum(objects/object/items/item[not(product_type='configurable' and
count(custom_options/custom_option[option_id='2686']) >
0)]/custom_options/custom_option[option_id='2686']/value)" />
You probably want to change it to this...
<xsl:variable name="items">
<xsl:for-each select="objects/object/items/item[not(product_type='configurable' and count(custom_options/custom_option[option_id='2686']) > 0)]">
<item>
<xsl:value-of select="qty_ordered * custom_options/custom_option[option_id='2686']/value" />
</item>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="sum(exslt:node-set($items)/item)" />
Hi Tim, thank you for the quick answer! I did indeed try it as this and it worked, but how do I implement this in an already existing code? Like does the
<xsl:template match="items">
matter? Because it's already something different. Sorry i'm new to XSL.– RsDob
2 days ago
<xsl:template match="items">
I've amended my answer to try and show how to implement it, but I suggest you spend some time learning about XSL first. See stackoverflow.com/questions/3511759/…
– Tim C
2 days ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Are you able to use XSLT 2.0? Also, it might help if you edited your question to show a sample of your input XML. Thanks!
– Tim C
Jun 29 at 8:51