如何从JasperReports报表项列表中的父级兄弟元素中通过id获取数据(xml datasource)

时间:2017-07-25 14:25:51

标签: xml xpath jasper-reports

我想创建一个订单商品列表;对于每个项目,我想显示“项目ID”和“地址行”字段;前一个字段的数据来自项目上的子元素,但后者的数据必须使用项目中引用的唯一ID从项目父项的兄弟中获取。

我已经创建了一个简化的xml数据源并报告,以说明问题。 xml数据定义如下:

<data>
<productorder>
    <item>
        <id>10001</id>
        <installationAddressId>1</installationAddressId>
    </item>
    <item>
        <id>10002</id>
        <installationAddressId>3</installationAddressId>
    </item>
</productorder>
<address>
    <id>1</id>
    <addressLine>Street 1, 12345 Berlin Germany</addressLine>
</address>
<address>
    <id>2</id>
    <addressLine>Street 2, 12345 Berlin Germany</addressLine>
</address>
<address>
    <id>3</id>
    <addressLine>Street 3, 12345 Berlin Germany</addressLine>
</address>
</data>

简单(不正确)的报告是:

<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="SimpleOrderItemReport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0d316a3a-9d1c-4483-bf7d-d1bd9165e0cb">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="SimpleOrderItemReportDataAdapter"/>
<subDataset name="OrderItemList" uuid="c1a7dc7b-2bc3-4f1f-bab9-ae6e3b207739">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="SimpleOrderItemReportDataAdapter"/>
    <queryString language="xPath">
        <![CDATA[/data/productorder/item]]>
    </queryString>
    <field name="id_1" class="java.lang.String">
        <fieldDescription><![CDATA[id]]></fieldDescription>
    </field>
    <field name="installationAddressId_1" class="java.lang.String">
        <fieldDescription><![CDATA[installationAddressId]]></fieldDescription>
    </field>
    <field name="addressLine" class="java.lang.String">
        <fieldDescription><![CDATA[../../address/addressLine]]></fieldDescription>
    </field>
</subDataset>
<queryString language="xPath">
    <![CDATA[/data]]>
</queryString>
<detail>
    <band height="125" splitType="Stretch">
        <componentElement>
            <reportElement x="124" y="19" width="390" height="30" uuid="84a1e8b2-13df-4391-9cdd-39a9d4d79434"/>
            <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">
                <datasetRun subDataset="OrderItemList" uuid="0d232993-a132-45ff-813c-d00d8a685d9e">
                    <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("/data/productorder/item")]]></dataSourceExpression>
                </datasetRun>
                <jr:listContents height="30" width="390">
                    <textField>
                        <reportElement x="40" y="0" width="40" height="30" uuid="2b9a8530-32e3-4ecb-8dc4-3d8d9d30b191"/>
                        <textElement textAlignment="Left" verticalAlignment="Top"/>
                        <textFieldExpression><![CDATA[$F{id_1}]]></textFieldExpression>
                    </textField>
                    <textField>
                        <reportElement x="190" y="0" width="200" height="30" uuid="d570993d-201b-4a43-9147-73036dd0cf11"/>
                        <textElement textAlignment="Left" verticalAlignment="Top"/>
                        <textFieldExpression><![CDATA[$F{addressLine}]]></textFieldExpression>
                    </textField>
                    <staticText>
                        <reportElement x="0" y="0" width="40" height="30" uuid="b96b0be3-fa58-42f1-8270-004a08a7c3df"/>
                        <textElement verticalAlignment="Top"/>
                        <text><![CDATA[Item ID:]]></text>
                    </staticText>
                    <staticText>
                        <reportElement x="90" y="0" width="100" height="30" uuid="f85e298c-e3e0-42e3-bd0d-fc7c7808c2f1"/>
                        <textElement verticalAlignment="Top"/>
                        <text><![CDATA[Address Line:]]></text>
                    </staticText>
                </jr:listContents>
            </jr:list>
        </componentElement>
    </band>
</detail>
</jasperReport>

对于上面提供的xml数据,我想在报告中看到以下两行:

  1. 项目编号:10001地址:1号街12345柏林德国
  2. 项目编号:10002地址:3号街12345柏林德国
  3. 我似乎无法找到可实现此目标的字段描述或xpath查询。有谁知道怎么做才能得到我想要的结果?

1 个答案:

答案 0 :(得分:1)

如果将JasperReports与Xalan一起用作XPath执行者(并且这是默认配置,请参阅net.sf.jasperreports.xpath.executer.factory),您可以使用XSLT current函数来引用当前节点。 / p>

在您的情况下,该字段应如下所示:

<field name="addressLine" class="java.lang.String">
    <fieldDescription><![CDATA[../../address[id = current()/installationAddressId]/addressLine]]></fieldDescription>
</field>