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>
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
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
1 Answer:
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
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:
Cheers