Jump to content
Changes to the Jaspersoft community edition download ×

How to sum repeated list elements?


membersound

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.

Example:

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

/list/customer[type=business]/amount[/code]

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?

Thanks

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

Hi,

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

XML_DataAdaper.png.47f1d9cf75c6c2ea2bd27efc1157d8de.png

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

XML_DataAdaper_all.png.3e3c220ecfe4f2ad1cdc9ab8f9525b08.png

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

XML_DataAdaper_customer.png.6304a26c65f76373281bb72cab0cf385.png

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)

XML_DataAdaper_summary.png.13247ab89a5859fae7d622d4b62f1fed.png

So if we ran this:

XML_DataAdaper_final.png.90bc343c417927434048829c7f5adaae.png

Cheers

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...