getting the total weight of unique prodid
Question
Hi everyone I need your help! I have been working on a code that will get the total weight of every unique product id that belong to a second level loop. Here's the code I am using. But the output I am getting is 110, which should be 60 only. Thank you in advance!
<xforms:bind calculate="sum(instance('Generated')/page1/table1/item[sublinesA[not(sublineA/prodid = preceding-sibling::sublinesA/sublineA/prodid)]]/sublinesA/sublineA[not(prodid = preceding-sibling::sublineA/prodid)]/weight)" nodeset"instance('Generated')/page1/table1/totalwgt"></xforms:bind>
<item>
<sublinesA>
<sublineA>
<prodid>AAA</prodid>
<weight>10</weight>
</sublineA>
<sublineA>
<prodid>AAA</prodid>
<weight>10</weight>
</sublineA>
<sublineA>
<prodid>BBB</prodid>
<weight>20</weight>
</sublineA>
</sublinesA>
</item>
<item>
<sublinesA>
<sublineA>
<prodid>BBB</prodid>
<weight>20</weight>
</sublineA>
<sublineA>
<prodid>BBB</prodid>
<weight>20</weight>
</sublineA>
<sublineA>
<prodid>CCC</prodid>
<weight>30</weight>
</sublineA>
</sublinesA>
</item>
<item>
<sublinesA>
<sublineA>
<prodid>CCC</prodid>
<weight>30</weight>
</sublineA>
</sublinesA>
</item>
No correct solution
OTHER TIPS
Your calculate
expression selects every sublinesA / sublineA / weight
element whose parent sublineA
element has a product ID different from that of its preceding sibling. So the first BBB
in the second item
is counted, as is the CCC
in the third item.
If you are intending to sum the weight by item, then you are wrong to expect the sum 60; you should be expecting the three sums 30, 50, 30.
If on the other hand you are intending to sum the weight for all items in a given table (and given how the calculation is bound, I guess you are), then [not(prodid = preceding-sibling::sublineA/prodid)]
is not the right predicate.
If there is only one table1
instance in the instance, then you can just substitute [not(prodid = preceding::sublineA/prodid)]
. If there is more than one, you are going to need either to do some very clever XPath, or possibly restructure the document to make it easier to do what you need.
(It's not relevant to your question, but I note in passing that you are checking the predicate twice; unless I'm missing something, the first doubly nested predicate can be dropped without changing the meaning of the expression. Unless your XForms engine is really aggressive about optimization, I expect the only effect of the first predicate is to slow things down.)