Jasper Report - Count nodes with particular value

2019-08-21 02:37发布

问题:

I have a Jasper Report with multiple subreports, using an XML datasource.

I am currently doing some visual enhancements and I am stuck with the Sub totals (Column footer) on the 1 Sub report. The parent report has a Group for keeping the linked records together. There is a sub report in the Detail band that has all the individual records.

Now comes the tricky part, I only want the sub total to display on the sub report, if the Group on the parent report contains more than 1 record.

I have tried using a Group COUNT variable, but the problem is that COUNT only increments as each record is processed. So the count is still 1 when the 1st subreport is generated.

I have tried using the XPath count() function, but I have not got it right. I have got the following in a field on the parent report (passing it as a parameter to subreport)

count(//CurrentNode[TargetNode = '$F{TargetNode}'])

I have tried various things, but it seems to be that I cannot use a Parameter or Field value in the XPath of the count() function. If I explicitly set a value instead of $F{TargetNode}, then I get the correct count of the nodes, but I need this count to change based on the current Group.

Is there another way to count the records in the Group, before generating the 1st subreport?

回答1:

After trying a few different approaches, I decided to count the records in each group, using the XPath count function in my XSL stylesheet. I was already ordering my records in this way, so it made sense.

I added an additional tag to store this record count.

The following expression counts records that share a common value between 2 different tags:

count(//Name[text() = current()/Name and ..//Surname[text() = current()/Surname]])