iReport:子报告(XPath XML数据源)不迭代记录

时间:2011-11-21 16:24:36

标签: xml jasper-reports ireport

我有子报告正常工作,但只有每个子报告中的第一项被反复显示。

So data formatted like this:           Looks like this

    A                                   A
      1                                   1
      2                                   1
      3                                   1
    B                                   B
      4                                   4
      5                                   4
      6                                   4
    C                                   C
      7                                   7 
      8                                   7

主报告模板:

<?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="Journey Report" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="ireport.zoom" value="1.4641000000000006"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["/home/stuart/"]]></defaultValueExpression>
    </parameter>
    <queryString language="xPath">
        <![CDATA[/clsXMLJourneyReport/JourneyList/clsXMLJourney]]>
    </queryString>
    <field name="ID" class="java.lang.String">
        <fieldDescription><![CDATA[ID]]></fieldDescription>
    </field>
    <field name="dateFrom" class="java.lang.String">
        <fieldDescription><![CDATA[dateFrom]]></fieldDescription>
    </field>
    <field name="dateTo" class="java.lang.String">
        <fieldDescription><![CDATA[dateTo]]></fieldDescription>
    </field>
    <field name="locFrom" class="java.lang.String">
        <fieldDescription><![CDATA[locFrom]]></fieldDescription>
    </field>
    <field name="locTo" class="java.lang.String">
        <fieldDescription><![CDATA[locTo]]></fieldDescription>
    </field>
    <field name="distance" class="java.lang.String">
        <fieldDescription><![CDATA[distance]]></fieldDescription>
    </field>
    <field name="journeyTime" class="java.lang.String">
        <fieldDescription><![CDATA[journeyTime]]></fieldDescription>
    </field>
    <field name="aveSpeed" class="java.lang.String">
        <fieldDescription><![CDATA[aveSpeed]]></fieldDescription>
    </field>
    <field name="distanceCF" class="java.lang.String">
        <fieldDescription><![CDATA[distanceCF]]></fieldDescription>
    </field>
    <field name="idleTime" class="java.lang.String">
        <fieldDescription><![CDATA[idleTime]]></fieldDescription>
    </field>
    <field name="Title1" class="java.lang.String">
        <fieldDescription><![CDATA[/clsXMLJourneyReport/Title1]]></fieldDescription>
    </field>
    <field name="Title2" class="java.lang.String">
        <fieldDescription><![CDATA[/clsXMLJourneyReport/Title2]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <image scaleImage="FillFrame">
                <reportElement x="0" y="0" width="555" height="79"/>
                <imageExpression><![CDATA["/home/stuart/Documents/Logic Ethos/Images/reportBackground1.jpg"]]></imageExpression>
            </image>
            <textField>
                <reportElement x="7" y="27" width="508" height="22"/>
                <textElement>
                    <font size="18"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{Title1}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="7" y="59" width="508" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{Title2}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <pageHeader>
        <band height="25" splitType="Stretch">
            <textField>
                <reportElement x="435" y="5" width="80" height="20"/>
                <textElement textAlignment="Right">
                    <font size="8"/>
                </textElement>
                <textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Report">
                <reportElement x="515" y="5" width="40" height="20"/>
                <textElement>
                    <font size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
        </band>
    </pageHeader>
    <detail>
        <band height="114" splitType="Stretch">
            <rectangle radius="10">
                <reportElement mode="Opaque" x="0" y="0" width="555" height="43" backcolor="#98E4FF"/>
            </rectangle>
            <textField>
                <reportElement x="134" y="4" width="411" height="10"/>
                <textElement>
                    <font fontName="SansSerif" size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{locFrom}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="134" y="17" width="411" height="11"/>
                <textElement>
                    <font fontName="SansSerif" size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{locTo}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="34" y="4" width="100" height="10"/>
                <textElement>
                    <font fontName="SansSerif" size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{dateFrom}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="34" y="17" width="100" height="11"/>
                <textElement>
                    <font fontName="SansSerif" size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{dateTo}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="7" y="4" width="26" height="10"/>
                <textElement>
                    <font fontName="SansSerif" size="8"/>
                </textElement>
                <text><![CDATA[From:]]></text>
            </staticText>
            <staticText>
                <reportElement x="7" y="17" width="26" height="11"/>
                <textElement>
                        <font fontName="SansSerif" size="8"/>
                </textElement>
                <text><![CDATA[To:]]></text>
            </staticText>
            <subreport>
                <reportElement x="7" y="44" width="538" height="69"/>
                <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("//XMLJourneyItem")]]></dataSourceExpression>
                <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "Journey Report_GPS.jasper"]]></subreportExpression>
            </subreport>
            <textField>
                <reportElement x="488" y="31" width="70" height="15"/>
                <textElement>
                    <font size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{distance}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="443" y="31" width="42" height="15"/>
                <textElement>
                    <font size="8"/>
                </textElement>
                <text><![CDATA[odometer:]]></text>
            </staticText>
            <textField>
                <reportElement x="207" y="31" width="60" height="16"/>
                <textElement>
                    <font size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{idleTime}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="184" y="31" width="23" height="15"/>
                <textElement>
                    <font size="8"/>
                </textElement>
                <text><![CDATA[Idle:]]></text>
            </staticText>
            <textField>
                <reportElement x="414" y="31" width="29" height="20"/>
                <textElement>
                    <font size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{distanceCF}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="379" y="31" width="46" height="20"/>
                <textElement>
                    <font size="8"/>
                </textElement>
                <text><![CDATA[crow-fly:]]></text>
            </staticText>
            <textField>
                <reportElement x="97" y="31" width="100" height="16"/>
                <textElement>
                    <font size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{journeyTime}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="35" y="31" width="62" height="16"/>
                <textElement>
                    <font size="8"/>
                </textElement>
                <text><![CDATA[Journey Time:]]></text>
            </staticText>
            <textField>
                <reportElement x="325" y="31" width="38" height="20"/>
                <textElement>
                    <font size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{aveSpeed}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="276" y="31" width="54" height="12"/>
                <textElement>
                    <font size="8"/>
                </textElement>
                <text><![CDATA[Ave Speed:]]></text>
            </staticText>
        </band>
    </detail>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

子报告模板:

<?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="Journey Report_GPS" language="groovy" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
    <property name="ireport.zoom" value="1.3310000000000004"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <queryString language="xPath">
        <![CDATA[/clsXMLJourneyReport/JourneyList/clsXMLJourney/JourneyItems/XMLJourneyItem]]>
    </queryString>
    <field name="date" class="java.lang.String">
        <fieldDescription><![CDATA[/clsXMLJourney/JourneyItems/XMLJourneyItem/date]]></fieldDescription>
    </field>
    <field name="status" class="java.lang.String">
        <fieldDescription><![CDATA[/clsXMLJourney/JourneyItems/XMLJourneyItem/status]]></fieldDescription>
    </field>
    <field name="odo" class="java.lang.String">
        <fieldDescription><![CDATA[/clsXMLJourney/JourneyItems/XMLJourneyItem/odo]]></fieldDescription>
    </field>
    <field name="loc" class="java.lang.String">
        <fieldDescription><![CDATA[/clsXMLJourney/JourneyItems/XMLJourneyItem/loc]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <columnHeader>
        <band height="28" splitType="Stretch">
            <staticText>
                <reportElement x="2" y="15" width="100" height="12"/>
                <textElement>
                    <font fontName="Ubuntu" size="8"/>
                </textElement>
                <text><![CDATA[Time]]></text>
            </staticText>
            <staticText>
                <reportElement x="102" y="15" width="100" height="12"/>
                <textElement>
                    <font fontName="Ubuntu" size="8"/>
                </textElement>
                <text><![CDATA[Location]]></text>
            </staticText>
            <staticText>
                <reportElement x="376" y="16" width="100" height="12"/>
                <textElement>
                    <font fontName="Ubuntu" size="8"/>
                </textElement>
                <text><![CDATA[Status]]></text>
            </staticText>
            <staticText>
                <reportElement x="476" y="16" width="100" height="12"/>
                <textElement>
                    <font fontName="Ubuntu" size="8"/>
                </textElement>
                <text><![CDATA[Odometer]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="16" splitType="Stretch">
            <textField>
                <reportElement x="2" y="4" width="100" height="12"/>
                <textElement>
                    <font fontName="Ubuntu" size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{date}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="102" y="4" width="274" height="12"/>
                <textElement>
                    <font fontName="Ubuntu" size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{loc}]]></textFieldExpression>
            </textField>
            <textField isBlankWhenNull="true">
                <reportElement x="376" y="4" width="100" height="12"/>
                <textElement>
                    <font fontName="Ubuntu" size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{status}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="476" y="4" width="100" height="12"/>
                <textElement>
                    <font fontName="Ubuntu" size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{odo}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

1 个答案:

答案 0 :(得分:2)

如果输入的xml数据如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<clsXMLJourneyReport>
    <Title1>Title</Title1>
    <Title2>Long title</Title2>
    <JourneyList>
        <clsXMLJourney>
            <ID>100</ID>
            <dateFrom>20.10.2010</dateFrom>
            <dateTo>20.12.2010</dateTo>
            <locFrom>21.11.2010</locFrom>
            <locTo>29.11.2010</locTo>
            <distance>122</distance>
            <journeyTime>27.11.2010</journeyTime>
            <aveSpeed>350</aveSpeed>
            <distanceCF>110</distanceCF>
            <idleTime>20.03.2011</idleTime>
            <JourneyItems>
                <XMLJourneyItem>
                    <date>21.03.2011</date>
                    <status>NEW</status>
                    <odo>odo1</odo>
                    <loc>loc1</loc>
                </XMLJourneyItem>
                <XMLJourneyItem>
                    <date>22.03.2011</date>
                    <status>NEW</status>
                    <odo>odo2</odo>
                    <loc>loc2</loc>
                </XMLJourneyItem>
                <XMLJourneyItem>
                    <date>23.03.2011</date>
                    <status>COORD</status>
                    <odo>odo3</odo>
                    <loc>loc3</loc>
                </XMLJourneyItem>
            </JourneyItems>
        </clsXMLJourney>
        <clsXMLJourney>
            <ID>200</ID>
            <dateFrom>18.09.2010</dateFrom>
            <dateTo>25.09.2010</dateTo>
            <locFrom>15.09.2010</locFrom>
            <locTo>17.09.2010</locTo>
            <distance>300</distance>
            <journeyTime>10.09.2010</journeyTime>
            <aveSpeed>330</aveSpeed>
            <distanceCF>120</distanceCF>
            <idleTime>25.03.2011</idleTime>
            <JourneyItems>
                <XMLJourneyItem>
                    <date>25.03.2011</date>
                    <status>NEW</status>
                    <odo>odo2_1</odo>
                    <loc>loc2_1</loc>
                </XMLJourneyItem>
                <XMLJourneyItem>
                    <date>25.03.2011</date>
                    <status>SIGN</status>
                    <odo>odo2_2</odo>
                    <loc>loc2_2</loc>
                </XMLJourneyItem>
            </JourneyItems>
        </clsXMLJourney>
    </JourneyList>
</clsXMLJourneyReport>

您可以在子报表中进行此修改:

<queryString language="xPath">
    <![CDATA[*]]>
</queryString>
<field name="date" class="java.lang.String">
    <fieldDescription><![CDATA[date]]></fieldDescription>
</field>
<field name="status" class="java.lang.String">
    <fieldDescription><![CDATA[status]]></fieldDescription>
</field>
<field name="odo" class="java.lang.String">
    <fieldDescription><![CDATA[odo]]></fieldDescription>
</field>
<field name="loc" class="java.lang.String">
    <fieldDescription><![CDATA[loc]]></fieldDescription>
</field>

结果将是:

enter image description here