如何从JasperReports报表项列表中的父级兄弟元素中通过id获取数据(xml datasource) [英] How to fetch data by id from parent's sibling element in JasperReports report items list (xml datasource)

查看:104
本文介绍了如何从JasperReports报表项列表中的父级兄弟元素中通过id获取数据(xml datasource)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create a list of order items; for each item, I want to display an "Item ID" and "Address Line" field; the data of the former field comes from a child element on the item, but the data from the latter must be fetched from a sibling of the item's parent using an unique id that is referenced in the item.

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

I've created a simplified xml data source and report in in order to illustrate the problem. The xml data is defined as follows:

<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>

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

The simple (incorrect) report is:

<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数据,我想在报告中看到以下两行:

For the xml data that was provided above, I want to see the following two rows in the report:


  1. 物品ID:10001地址:1号街12345柏林德国

  2. 物品ID:10002地址行:Street 3,12345 Berlin Germany

我似乎无法找到可实现此目标的字段描述或xpath查询。有没有人知道如何获得我想要的结果?

I can't seem to find a field description or xpath query that would achieve this goal. Does anyone know how to do get my desired result?

推荐答案

如果你使用JasperReports和Xalan作为XPath执行者(这是默认的)配置,请参阅 net.sf.jasperreports.xpath .executer.factory ),您可以使用XSLT 当前函数来引用当前节点。

If you use JasperReports with Xalan as XPath executer (and that's the default configuration, see net.sf.jasperreports.xpath.executer.factory), you can use the XSLT current function to refer to the current node.

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

In your case, the field should look like this:

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

这篇关于如何从JasperReports报表项列表中的父级兄弟元素中通过id获取数据(xml datasource)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆