有关XML数据源的JasperReports报告:如何通过嵌套项列表中父级兄弟元素的ID获取数据

时间:2017-10-02 08:49:31

标签: xml xpath jasper-reports datasource

我想创建一个订单商品列表;对于每个项目,我想显示所有子项目的列表。在子项目列表中,我想显示有关项目及其链接的信息 - >子项目可以包含父项兄弟姐妹的子项目的链接(链接项目的itemId在//links/itemId字段中提供

我希望我可以通过将父项的itemId传递给嵌套列表来实现此结果,但是,我已经做到了。我将在下面举一个例子。 示例XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<order>
    <items>
        <itemId>1</itemId>
        <name>Item1</name>
        <offeringRef>offering1</offeringRef>
        <children>
            <items>
                <itemId>2</itemId>
                <name>Item2</name>
                <offeringRef>offering2</offeringRef>
                <links>
                    <linkName>toOffering4</linkName>
                    <itemId>9</itemId>
                </links>
                <links>
                    <linkName>toOffering3</linkName>
                    <itemId>3</itemId>
                </links>
            </items>
            <items>
                <itemId>3</itemId>
                <name>Item3</name>
                <offeringRef>offering2</offeringRef>
                <links>
                    <linkName>toOffering4</linkName>
                    <itemId>8</itemId>
                </links>
            </items>
        </children>
    </items>
    <items>
        <itemId>4</itemId>
        <name>Item4</name>
        <offeringRef>offering1</offeringRef>
        <children>
            <items>
                <itemId>5</itemId>
                <name>Item5</name>
                <offeringRef>offering2</offeringRef>
                <links>
                    <linkName>toOffering4</linkName>
                    <itemId>10</itemId>
                </links>
            </items>
            <items>
                <itemId>6</itemId>
                <name>Item6</name>
                <offeringRef>offering2</offeringRef>
            </items>
        </children>
    </items>
    <items>
        <itemId>7</itemId>
        <name>Item7</name>
        <offeringRef>offering3</offeringRef>
        <children>
            <items>
                <itemId>8</itemId>
                <name>Item8</name>
                <offeringRef>offering4</offeringRef>
            </items>
            <items>
                <itemId>9</itemId>
                <name>Item9</name>
                <offeringRef>offering4</offeringRef>
            </items>
            <items>
                <itemId>10</itemId>
                <name>Item10</name>
                <offeringRef>offering4</offeringRef>
            </items>
            <items>
                <itemId>11</itemId>
                <name>Item11</name>
                <offeringRef>offering5</offeringRef>
            </items>
        </children>
    </items>
</order>

示例JRXML:

<?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="OrderReport-Links" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="b748b258-915c-41ff-842c-9deb952cf667">
    <subDataset name="Offering1List" uuid="97b9effb-88fa-431e-83ef-1c4f48f43b2a">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="OrderWithLinks"/>
        <parameter name="CURRENT_ITEM_ID" class="java.lang.String" evaluationTime="Late">
            <parameterDescription><![CDATA[]]></parameterDescription>
            <defaultValueExpression><![CDATA["/order/items[offeringRef='offering1']"]]></defaultValueExpression>
        </parameter>
        <parameter name="PATH_TO_OFFERING2" class="java.lang.String" evaluationTime="Late">
            <parameterDescription><![CDATA[]]></parameterDescription>
            <defaultValueExpression><![CDATA["/order/items[offeringRef='offering1']/children/items[offeringRef='offering2']"]]></defaultValueExpression>
        </parameter>
        <queryString language="xPath">
            <![CDATA[/order/items[offeringRef='offering1']]]>
        </queryString>
        <field name="itemId_1" class="java.lang.String">
            <fieldDescription><![CDATA[itemId]]></fieldDescription>
        </field>
        <field name="name_1" class="java.lang.String">
            <fieldDescription><![CDATA[name]]></fieldDescription>
        </field>
        <field name="offeringRef_1" class="java.lang.String">
            <fieldDescription><![CDATA[offeringRef]]></fieldDescription>
        </field>
        <variable name="V_CURRENT_ITEM_ID" class="java.lang.String">
            <variableExpression><![CDATA[((org.w3c.dom.Node)((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).getSelectObject(((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).getCurrentNode() , "itemId/text()")).getNodeValue().toString()]]></variableExpression>
        </variable>
        <variable name="V_PARENT_OF_CURRENT_NODE" class="java.lang.String">
            <variableExpression><![CDATA[((org.w3c.dom.Node)((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).getSelectObject(((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).getCurrentNode() , "../../itemId/text()")).getNodeValue().toString()]]></variableExpression>
        </variable>
        <variable name="V_ITEM_ID" class="java.lang.String">
            <variableExpression><![CDATA[((org.w3c.dom.Node)((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).getSelectObject(((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).getCurrentNode() , "itemId/text()")).getNodeValue().toString()]]></variableExpression>
        </variable>
    </subDataset>
    <subDataset name="Offering2List" uuid="76bc8192-4801-4f2c-8e03-89f8a38a588b">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="OrderWithLinks"/>
        <parameter name="PATH_TO_OFFERING2" class="java.lang.String" evaluationTime="Late">
            <defaultValueExpression><![CDATA["/order/items[offeringRef='offering1']/children/items[offeringRef='offering2']"]]></defaultValueExpression>
        </parameter>
        <queryString language="xPath">
            <![CDATA[/order/items[offeringRef='offering1']/children/items[offeringRef='offering2']]]>
        </queryString>
        <field name="itemId_1" class="java.lang.String">
            <fieldDescription><![CDATA[itemId]]></fieldDescription>
        </field>
        <field name="name_1" class="java.lang.String">
            <fieldDescription><![CDATA[name]]></fieldDescription>
        </field>
        <field name="offeringRef_1" class="java.lang.String">
            <fieldDescription><![CDATA[offeringRef]]></fieldDescription>
        </field>
        <field name="parentItemId" class="java.lang.String">
            <fieldDescription><![CDATA[../../itemId]]></fieldDescription>
        </field>
        <field name="Offering4ItemName" class="java.lang.String">
            <fieldDescription><![CDATA[../../../items[offeringRef='offering3']/children/items[offeringRef='offering4' and itemId=current()/links[linkName='toOffering4']/itemId]/name]]></fieldDescription>
        </field>
        <variable name="V_CURRENT_ITEM_ID" class="java.lang.String">
            <variableExpression><![CDATA[((org.w3c.dom.Node)((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).getSelectObject(((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).getCurrentNode() , "itemId/text()")).getNodeValue().toString()]]></variableExpression>
        </variable>
        <variable name="V_PARENT_OF_CURRENT_NODE" class="java.lang.String">
            <variableExpression><![CDATA[$V{V_PARENT_OF_CURRENT_NODE}]]></variableExpression>
        </variable>
        <variable name="ITEM_ID" class="java.lang.String">
            <variableExpression><![CDATA[$V{ITEM_ID}]]></variableExpression>
        </variable>
    </subDataset>
    <parameter name="CURRENT_ITEM_ID" class="java.lang.String" isForPrompting="false" evaluationTime="Late">
        <defaultValueExpression><![CDATA["/order/items[offeringRef='offering1']"]]></defaultValueExpression>
    </parameter>
    <parameter name="PATH_TO_OFFERING2" class="java.lang.String" isForPrompting="false" evaluationTime="Late">
        <defaultValueExpression><![CDATA["/order/items[offeringRef='offering1']/children/items[offeringRef='offering2']"]]></defaultValueExpression>
    </parameter>
    <queryString language="xPath">
        <![CDATA[/order]]>
    </queryString>
    <field name="itemId" class="java.lang.String">
        <fieldDescription><![CDATA[itemId]]></fieldDescription>
    </field>
    <field name="name" class="java.lang.String">
        <fieldDescription><![CDATA[name]]></fieldDescription>
    </field>
    <field name="offeringRef" class="java.lang.String">
        <fieldDescription><![CDATA[offeringRef]]></fieldDescription>
    </field>
    <variable name="V_MAIN_CURRENT_ITEM_ID" class="java.lang.String">
        <variableExpression><![CDATA[((org.w3c.dom.Node)((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).getSelectObject(((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).getCurrentNode() , "itemId/text()")).getNodeValue().toString()]]></variableExpression>
    </variable>
    <detail>
        <band height="125" splitType="Stretch">
            <componentElement>
                <reportElement x="0" y="10" width="560" height="60" uuid="021a30b2-8d3f-4962-b222-1cc61c257058"/>
                <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
                    <datasetRun subDataset="Offering1List" uuid="1b1d83bc-81d3-4694-8ec5-ee68916237e9">
                        <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).dataSource("/order/items[offeringRef='offering1']")]]></dataSourceExpression>
                    </datasetRun>
                    <jr:listContents height="60" width="560">
                        <frame>
                            <reportElement x="0" y="0" width="560" height="60" uuid="6eb14281-c6c6-41a3-9579-5d2a9a8a52cb"/>
                            <box>
                                <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                            </box>
                            <textField>
                                <reportElement x="0" y="0" width="140" height="15" uuid="3a0d8007-ac1e-4686-99b3-8efe2db3e948">
                                </reportElement>
                                <textFieldExpression><![CDATA["Offering: "+$F{offeringRef_1}]]></textFieldExpression>
                            </textField>
                            <textField>
                                <reportElement x="140" y="0" width="100" height="15" uuid="ec93e06d-4e71-42c6-97b9-73e2fe37c557">
                                </reportElement>
                                <textFieldExpression><![CDATA["Item Name: " + $F{name_1}]]></textFieldExpression>
                            </textField>
                            <textField>
                                <reportElement x="240" y="0" width="100" height="15" uuid="f6ea5cbc-3708-42b1-bede-9d13cffdf7c2">
                                </reportElement>
                                <textFieldExpression><![CDATA["Item ID: "+$F{itemId_1}]]></textFieldExpression>
                            </textField>
                            <componentElement>
                                <reportElement x="41" y="31" width="519" height="30" uuid="70a6f387-6b4f-4346-8d0c-ecd65dd4cd05"/>
                                <jr:list printOrder="Vertical">
                                    <datasetRun subDataset="Offering2List" uuid="e41844dc-e580-4395-858f-2e9231c6de72">
                                        <datasetParameter name="PATH_TO_OFFERING2">
                                            <datasetParameterExpression><![CDATA["/order/items[offeringRef='offering1' and itemId=" + $V{V_PARENT_OF_CURRENT_NODE}+ "]/children/items[offeringRef='offering2' and itemId=" + $V{V_CURRENT_ITEM_ID} + "]"]]></datasetParameterExpression>
                                        </datasetParameter>
                                        <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).dataSource($P{PATH_TO_OFFERING2})]]></dataSourceExpression>
                                    </datasetRun>
                                    <jr:listContents height="30" width="519">
                                        <frame>
                                            <reportElement x="0" y="0" width="519" height="30" uuid="b2cfd080-c19c-439b-8b18-9c6fe74879b5"/>
                                            <box>
                                                <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                                <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                                <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                                <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                            </box>
                                            <textField>
                                                <reportElement x="100" y="0" width="70" height="30" uuid="881df01d-4f99-44f7-8e2e-bbdd8a39be2f"/>
                                                <textFieldExpression><![CDATA["Item ID: "+ $V{V_CURRENT_ITEM_ID}]]></textFieldExpression>
                                            </textField>
                                            <textField>
                                                <reportElement x="170" y="0" width="100" height="30" uuid="174cbad9-4171-4ec8-aa6d-1f022f340d5b"/>
                                                <textFieldExpression><![CDATA["Item Name: "+ $F{name_1}]]></textFieldExpression>
                                            </textField>
                                            <textField>
                                                <reportElement x="0" y="0" width="100" height="30" uuid="d57ace7b-f0ef-499d-ba2b-893c1110750b"/>
                                                <textFieldExpression><![CDATA["Offering: "+$F{offeringRef_1}]]></textFieldExpression>
                                            </textField>
                                            <textField>
                                                <reportElement x="400" y="0" width="100" height="30" uuid="d43d3271-c481-4128-8647-feb198f32b68"/>
                                                <textFieldExpression><![CDATA["Parent Item ID: "+ $F{parentItemId} + " : " + $V{ITEM_ID}]]></textFieldExpression>
                                            </textField>
                                            <textField>
                                                <reportElement x="270" y="0" width="130" height="30" uuid="66324252-49fd-4d78-9830-d5ef7ec66600"/>
                                                <textFieldExpression><![CDATA["Offering 4 Name: "+ $F{Offering4ItemName}]]></textFieldExpression>
                                            </textField>
                                        </frame>
                                    </jr:listContents>
                                </jr:list>
                            </componentElement>
                            <textField>
                                <reportElement x="340" y="0" width="220" height="15" uuid="82201018-17ef-4eb7-91bb-d6ba4f86b13c">
                                </reportElement>
                                <textFieldExpression><![CDATA["Current Item ID (Variable): " + $V{V_ITEM_ID}]]></textFieldExpression>
                            </textField>
                            <frame>
                                <reportElement x="40" y="15" width="520" height="15" uuid="7dc8c9ce-a511-4e13-bb6b-647f05c1785a">
                                </reportElement>
                                <box>
                                    <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                    <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                                </box>
                                <staticText>
                                    <reportElement x="0" y="0" width="100" height="15" uuid="860a6ae0-798b-4e16-a920-3223d602eebb">
                                    </reportElement>
                                    <text><![CDATA[Childs Items:]]></text>
                                </staticText>
                            </frame>
                        </frame>
                    </jr:listContents>
                </jr:list>
            </componentElement>
        </band>
    </detail>
</jasperReport>

预期输出(没有用红线划掉的行)如下:

Expected output

有谁知道如何制作此报告?

注意:使用How to fetch data by id from parent's sibling element in JasperReports report items list (xml datasource)中的解决方案无法实现此,因为它依赖于使用subDataSource,而带有subDataSource的嵌套列表无法访问父级的兄弟节点(那是为什么在我的例子中我在嵌套列表的数据集描述中使用了dataSource

使用subDataSource /items/children/items[offeringRef='offering2']进行输出(问题已被圈出): Example of the problem with using subDataSource

1 个答案:

答案 0 :(得分:0)

将以下数据源用于嵌套列表:

<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).dataSource("/order/items[offeringRef='offering1' and itemId=" + $F{itemId_1}+ "]/children/items[offeringRef='offering2']")]]></dataSourceExpression>

有关详细信息,请参阅the Javadoc of the class