XmlDataSource and XPath question

Hi all,

I'm working with JasperReports for generating some pretty complex reports - and a combination of  an XML data source and subreports seems to do the trick quite well. Now I have a question that I can't seem to answer by myself.

A subreport is placed in the detail section, sporting a Data Source Expression which is a subDataSource of the original ReportDataSource; so far so good, it works and I can place the underlying parts I need onto the layout. But now the report end-user wants to place some related data that is in some other branch of the tree.

Imagine an XML structure like this:

<Report>

  <Header>

    <Specimen ID="123456">...<InternalId>0905-4478</InternalId>...</Specimen>

  </Header>

  <Data>

    <Result>...<SpecimenId>123456</SpecimenId>...</Result>

  </Data>

</Report>

The main report's expression is /Report/Data/Result. Inside its detail band is a subreport with DataSourceExpression:

  ((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("/Result")

Inside this subreport I have a field $F{SpecimenId} with description ./SpecimenId - which I can print, but the end-user would like to see the content of the related InternalId node.

The XPath expression to get there would be

  //Specimen[@ID='NN']/InternalId

where NN is the content of the field $F{SpecimenId}.

But I'm at a loss as to how I could define a variable which goes out to fetch this data.

Thanks in advance for any ideas and helpful hints!

 

Jan Schenkel.

janschenkel's picture
Joined: May 13 2009 - 5:58am
Last seen: 14 years 4 months ago

1 Answer:

Tthe only solution that I can think of is to use a further subreport to select and display the value.  In addition to this, you would have to change the data source of the first subreport so that it uses the whole XML document.

So you would do something like:

  • The first (existing) subreport would have a data source expression like $P{REPORT_DATA_SOURCE}.dataSource("/Report/Data/Result[ID = '" + $F{ResultID} + "']").
    I'm assuming here that the Result node has an ID.  If that's not the case, you can use the node index/context position by declaring a field that has position() as XPath expression, and using dataSource("/Report/Data/Result[position() = '" + $F{Position} + "']") for the subreport.
  • In the subreport, include a second subreport that uses $P{REPORT_DATA_SOURCE}.dataSource("//Specimen[@ID = " + $F{SpecimedID} + "']").

Regards,

Lucian

lucianc's picture
87377
Joined: Jul 17 2006 - 1:10am
Last seen: 22 hours 11 min ago
Feedback
randomness