Jump to content
Changes to the Jaspersoft community edition download ×

How to sum repeated list elements?


Recommended Posts

How can I select a node from XML datasource by condition? Eg I have an xml that has multiple customer nodes. I now want to select the first customer whose type=business.

Moreover, I want to sum up all values found fo the specific list elements.


<list>  <customer>   <type>business</type>   <amount>10</amount> <!-- type String.class -->  </customer>  <customer>   <type>private</type>   <amount>20</amount>  </customer></list>[/code]

What is the jrxml expression for this? Do I have to use XPATH to locate the element? If yes, I could think of:


But how can I ensure that these values are summed up?

And what if I just want to display these values repreatedly? How do I have to create the jrxml variables, fields and parameters to get automatically mapped from the xml?

Same question applies if I have a java bean. Probably the mechanism/logic behind summing a field up automatically is the same?


Link to comment
Share on other sites

  • 3 weeks later...
  • Replies 1
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted Images


i've created an XML file with your content.

Then created a new Data Adapter - XML Document, that points to the XML file with your content. Here what is important to note is to choose the option:

  • Use the report Xpath expression when filling the report


Afterwards, at the Report's dataset expression just choose everything within the document


Then created a new Dataset and selected only the data needed (in this case all the "customer" data)


With this new dataset i've added a table component to the report and assigned the newly created dataset to it. I've added a new Variable to the newly created dataset with the name Sum_Amount with the following expression: new java.math.BigDecimal($F{amount}), and the Calculation option as Sum.

So, defined the dataset run of the table with the dataset as the next image shows. (Here I choose the Don't use any connection or Data Source option)


So if we ran this:



Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...