. Retrieve data from an XML file with parameters in BIRT

BIRT reports must use data that is structured as a table consisting of rows and columns. XML documents use elements and attributes to present data. In BIRT, the data set wizard enables you to map a top-level XML element as a data set row, and other XML elements or attributes as columns. The wizard uses XPath expressions to define the paths to elements and attributes. XPath is a query language used to access parts of an XML document. When you select an element or attribute to map to a row or a column, the wizard generates the corresponding XPath expression. If you are familiar with XPath syntax and you want to do more than the basic mapping, you can write your own XPath expressions.

Here is the sample XML document:

<?xml version="1.0" encoding="utf-8"?>
<library> 
  <book category="COOKING"> 
    <title lang="es">The Spanish Cook Book</title>  
    <author name="Miguel Ortiz" country="es"/>  
    <year>2005</year> 
  </book>  
  <book category="CHILDREN"> 
    <title lang="en">Everyone is Super Special</title>  
    <author name="Sally Bush" country="us"/>  
    <year>2005</year> 
  </book>  
  <audio format="CD" category="MUSIC"> 
    <title lang="en">We All Sing Perty</title>  
    <artist name="Mary Rogers" country="us"/>  
    <year>2005</year> 
  </audio>  
  <audio format="CD" category="MUSIC"> 
    <title lang="en">The Bluest Blues</title>  
    <artist name="Barry Sadley" country="us"/>  
    <year>2005</year> 
  </audio> 
</library>

Define the column mapping, you can select the XML element or attribute that represents a column and add every column to the data set.

The figure shows an example of column mappings defined in a data set.

imagepng

You can set the xpath expression to library/book/[@category=“{?p1?}”], where p1 is the input parameter name.

If you want to use data that is structured as a table consisting of rows and columns and use report parameters as a query for JDBC implementation with report parameters:

select COLUMN_NAME_1,COLUMN_NAME_2 from T_TABLE where COLUMN_NAME_1=?  and COLUMN_NAME_2=?

You can use esProc as Datasource preparing data for BIRT report. esProc SPL’s implementation is like this:


A B C
1 =file("./book1.xml")

2 =xml@s(A1.read()) =book=null,audio=null
3 =A2.library

4 for   A3

5
if(A4.fname(1)=="book")
6

=book=if(book==null,  
create(category,${A4.book.conj(~.fname()).concat@c()}).record(A4.book.conj(~.array()).insert(1,A4.category)),
book.record(A4.book.conj(~.array()).insert(1,A4.category))  )
7
if(A4.fname(1)=="audio")
8

=audio=if(audio==null, 
create(category,${A4.audio.conj(~.fname()).concat@c()}).record(A4.audio.conj(~.array()).insert(1,A4.category)),
audio.record(A4.audio.conj( ~.array()).insert(1,A4.category))  )
9 =book=book.new(category,title,name,year)
10 =audio=audio.new(category,title,name,year)
11 return   book,audio

C6: Create a wide table under the book element.

imagepng

A9:

imagepng

The report can be designed in the same way as you would if you were retrieving the data from a database. For detail SPL integration with BIRT, see How to Call an SPL Script in BIRT. If you have any questions or comments please leave them below.