我在使用正确的XML结构输出XML数据(在DB2中)时遇到了一些麻烦。
这是一个带有WHERE子句的查询结果的示例,该子句类似于WHERE InvoiceNumber IN('383','385')......结果是5行
Inv# InvoiceDate InvoiceAmt Warehouse GLAccount PostDate GLAmount
383 20140526 391 02 020 20170526 72
383 20140526 391 02 030 20170526 252
383 20140526 391 02 040 20170526 67
385 20140527 324 02 020 20170526 72
385 20140527 324 02 030 20170526 252
前三行,因为它们是相同的发票号码(即383),应该产生1个发票标签(如下面的XML所示)。最后两行应该生成另一个发票标记(如下面的XML所示)。
该发票标签包含一个包含所有标题字段的标题标记(即3行之间的常用值,如Inv#,Invoice Date,Invoice Amount和Warehouse number)。
发票标签还包含详细信息标签,每个标签有3行(即帐户,发布日期和详细金额)。
因此,XML结构应该如下所示......
<Register> 002764
<Invoice>
<Header>
<InvoiceType>RI</InvoiceType>
<Inv#>383</Inv#>
<InvoiceDate>20170526</InvoiceDate>
<InvoiceAmount>391</InvoiceAmount>
<Warehouse>ICE01</Warehouse>
</Header>
<Detail>
<GLAccount>020</GLAccount>
<DetailedAmount>72</DetailedAmount>
<PostDate>20170501</PostDate>
</Detail>
<Detail>
<Account>030</Account>
<DetailedAmount>252</DetailedAmount>
<PostDate>20170526</PostDate>
</Detail>
<Detail>
<Account>040</Account>
<DetailedAmount>267</DetailedAmount>
<PostDate>20170526</PostDate>
</Detail>
</Invoice>
<Invoice>
<Header>
<InvoiceType>RI</InvoiceType>
<InvoiceNumber>385</InvoiceNumber>
<InvoiceDate>20170527</InvoiceDate>
<InvoiceAmount>324</InvoiceAmount>
<Warehouse>02</Warehouse>
</Header>
<Detail>
<GlAccount>020</GlAccount>
<GlAmount>72</GlAmount>
<PostingDate>20170526</PostingDate>
</Detail>
<Detail>
<GlAccount>030</GlAccount>
<GlAmount>252</GlAmount>
<PostingDate>20170526</PostingDate>
</Detail>
</Invoice>
</Register>
我正在尝试将Register标记完全包含所有发票标记,以生成上面显示的确切XML格式。
代码分别输出寄存器标记,而不是寄存器标记包含所有发票。像这样......
<Register>002764</Register>
<Invoice>
<Header>
<InvoiceType>RI</InvoiceType>
<Inv#>383</Inv#>
<InvoiceDate>20170526</InvoiceDate>
<InvoiceAmount>391</InvoiceAmount>
<Warehouse>ICE01</Warehouse>
</Header>
<Detail>
<GLAccount>020</GLAccount>
<DetailedAmount>72</DetailedAmount>
<PostDate>20170501</PostDate>
</Detail>
<Detail>
<Account>030</Account>
<DetailedAmount>252</DetailedAmount>
<PostDate>20170526</PostDate>
</Detail>
<Detail>
<Account>040</Account>
<DetailedAmount>267</DetailedAmount>
<PostDate>20170526</PostDate>
</Detail>
</Invoice>
<Invoice>
<Header>
<InvoiceType>RI</InvoiceType>
<InvoiceNumber>385</InvoiceNumber>
<InvoiceDate>20170527</InvoiceDate>
<InvoiceAmount>324</InvoiceAmount>
<Warehouse>02</Warehouse>
</Header>
<Detail>
<GlAccount>020</GlAccount>
<GlAmount>72</GlAmount>
<PostingDate>20170526</PostingDate>
</Detail>
<Detail>
<GlAccount>030</GlAccount>
<GlAmount>252</GlAmount>
<PostingDate>20170526</PostingDate>
</Detail>
</Invoice>
这是我的DB2代码..
SELECT
--XMLDOCUMENT(
-- XMLELEMENT(NAME "Receivables",
-- XMLFOREST(''P1'' AS CompanyCode),
-- XMLELEMENT(NAME "Register", IPGL_001),
XMLELEMENT ( NAME "Invoice",
XMLELEMENT ( NAME "Header",
XMLFOREST(
'RI' as InvoiceType,
trim(IPGL_002) AS InvoiceNumber,
IPGL_007 AS InvoiceDate,
SUM(IPGL_005) AS InvoiceAmount,
-- trim(CLIENT_119) AS ExternalClientCode,
-- trim(CLIENT_002) as ClientName,
-- CLIENT_003 AS ClientAddress1,
-- CLIENT_004 AS ClientAddress2,
-- trim(CLIENT_005) AS ClientAddress3,
-- CLIENT_006 AS ClientAddress4,
WHSE AS Warehouse
) -- end of forest
) -- end of header
, (SELECT XMLAgg (XMLELEMENT (NAME "Detail",
XMLFOREST (TRIM(IPGL_004) AS "GLAccount",
IPGL_005 AS "GLAmount",
IPGL_003 AS "PostingDate"
) -- END OF XMLFOREST
) -- END OF XMLELEMENT DETAIL
) -- END OF XMLAgg
FROM PW_IPGL_L1 AS DTL WHERE DTL.IPGL_002=IPGL.IPGL_002)
) -- end of invoice
-- ) -- end of Receivables root element
--) -- end of XMLDOCUMENT
FROM PW_IPGL_L1 AS IPGL
JOIN PW_CLIENT_L1 AS CLIENT
ON IPGL.IPGL_006 = CLIENT.CLIENT_001
JOIN (
SELECT RERH_002 AS InvoiceNumber, RERH_031 AS WHSE FROM PW_RERH_L1
UNION ALL
SELECT ACC1_002 AS InvoiceNumber, ACC1_017 AS WHSE FROM PW_ACC1_L1
UNION ALL
SELECT REC1_002 AS InvoiceNumber, REC1_018 AS WHSE FROM PW_REC1_L1
) AS INVOICES
ON IPGL.IPGL_002 = INVOICES.InvoiceNumber
WHERE IPGL_002 IN ('151125 ', '151124 ')
GROUP BY IPGL_001, IPGL_002, IPGL_007, WHSE
ORDER BY IPGL_002;