0

Here's my XML

<ticket><sold>0</sold><price>7.00</price></ticket>
<ticket><sold>1</sold><price>7.00</price></ticket>
<ticket><sold>1</sold><price>7.00</price></ticket>

I want to sum the total sale prices, in this case it will be $14.00 because the 1st one does not sell any ticket

the tag will only have 2 values (1 or 0)

The simplest way I think of is to multiple price with sold for each ticket and then sum it all up.

I group by and below is my code but I got 0

0<?sum(current-group()/sold * current-group()/price)?>

Do you know why? If you have a simpler approach, please let me know

Thanks

Andy
  • 195
  • 2
  • 5
  • 13

2 Answers2

1

Because from your try (sum(current-group()/sold * current-group()/price)) I assume you are looking for the sum of multiplication of sold and price. With xpath 2.0 you may try:

sum(//ticket[sold!=0]/(price * sold))
hr_117
  • 9,589
  • 1
  • 18
  • 23
0

If you know that the value of <sold> is only ever 0 or 1 you can just do:

sum(//ticket[sold!=0]/price[.>0])

The last check ([.>0]) will ensure they are numeric nodes to make sure it will return correctly.