Use a subreport in a report which is connected to an xml datasource

Table of Contents 

Subreports are great for many purposes. They are a great way to add data coming from a different datasource or to centralize a piece of a report that is the same in many reports. With subreports you don't need to repeat yourself or do copy/pastes; all you need to do is modify only one subreport instead of many master reports.


This has been verified and tested using iReport 4.0 and higher

The issue

What I've found is that when I use xml datasource, the data in the subreport doesn't appear. So I have my subreport using my XML datasource with a Xpath query and when I run it, my chart and table are displayed correctly, but when I include it in a master report and set the subreport to use the main datasource then no data appears.

The solution

Because of the nature of the datasource, the datasource isn't passed to the subreport in the usual way. When using an xml datasource you will notice that there are additional default parameters. One of them is XML_DATA_DOCUMENT.

  1. To make sure your data are passed to the subreport you need to go to the properties of the subreport and scrolling down to the bottom, you will find the Parameters property.
  2. Open it using the ellipsis button () and add a new link to pass a parameter to your subreport. In the Subreport parameter name field you need to enter XML_DATA_DOCUMENT and in the Value expression field pick $P\{XML_DATA_DOCUMENT}.

  3. Press OK twice
  4. In the sub-report properties you can also find the property Connection type
  5. Select Don't pass data in the Connection type.

This way the subreport will have access of the xml datasource and should display its content the same way as it does running on its own separatly using that XML datasource.