m_smith7895 Posted July 25, 2016 Share Posted July 25, 2016 Hi,Using iReports 5.1.0 I've been trying to figure out how to use subreports to sort the data within the following XML datasource:<PAGE> <DATA> <INVENTORY> <ITEM> <ITEM_NAME>FOO</ITEM_NAME> <ITEM_QTY>1</ITEM_QTY> <ITEM_TOTAL>$320</ITEM_TOTAL> <ORDER> <ORD>1</ORD> </ORDER> </ITEM> <ITEM> <ITEM_NAME>BAR</ITEM_NAME> <ITEM_QTY>1</ITEM_QTY> <ITEM_TOTAL>$400</ITEM_TOTAL> <ORDER> <ORD>4</ORD> </ORDER> </ITEM> <ITEM> <ITEM_NAME>CAN</ITEM_NAME> <ITEM_QTY>1</ITEM_QTY> <ITEM_TOTAL>$800</ITEM_TOTAL> <ORDER> <ORD>3</ORD> </ORDER> </ITEM> </INVENTORY> <EMP> <ITEM> <ITEM_NAME>PAR</ITEM_NAME> <ITEM_QTY>1</ITEM_QTY> <ITEM_TOTAL>$3.00</ITEM_TOTAL> <UNIT_PRICE>$3.00</UNIT_PRICE> <ORDER> <ORD>2</ORD> </ORDER> </ITEM> </EMP> </DATA></PAGE>The idea is to use the fields INVENTORY/ITEM/ORDER/ORD and EMP/ITEM/ORDER/ORD to sort so that the order they are printed by ITEM_NAME is: FOO (INVENTORY, ORD=1) PAR (EMP, ORD=2) CAN (INVENTORY, ORD=3) BAR (INVENTORY, ORD=4)Which corresponds to the order designated by the ORDER/ORD fields that exist within both INVENTORY and EMP ITEMs.So my question is, is there a way to read all ORD values within the XML and display the values of the surrounding ITEM fields(irrespective of whether it is an INVENTORY or EMP ITEM) line by line without editing the XML eg. NAME QTY TOTAL FOO 1 $320 PAR 1 $3.00 CAN 1 $800 BAR 1 $400 Currently they are displaying sequentially in the order they appear within the XML using 2 subreports, one referencing INVENTORY and another referencing EMP. Link to comment Share on other sites More sharing options...
Solution lucianc Posted July 26, 2016 Solution Share Posted July 26, 2016 You can use something like /PAGE/DATA/*/ITEM as XPath query, and then map a field to ORDER/ORD and use it as sort field:<?xml version="1.0" encoding="UTF-8"?><jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="xml_data" pageWidth="995" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"><queryString language="XPath">/PAGE/DATA/*/ITEM</queryString><field name="ITEM_NAME"> <fieldDescription>ITEM_NAME</fieldDescription></field><field name="ORD" class="java.lang.Integer"> <fieldDescription>ORDER/ORD</fieldDescription></field><sortField name="ORD"/><detail> <band height="20"> <textField> <reportElement width="200" y="0" x="0" height="20"/> <textFieldExpression>$F{ITEM_NAME} + " " + $F{ORD}</textFieldExpression> </textField> </band></detail></jasperReport>[/code]Regards,Lucian Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now