终于回答了这个问题 - 下面概述了它会帮助一些必须使用 XML 显式的其他不幸的灵魂:)
我有以下代码输出下面包含的 XML。我遇到的问题是,每个request_status_update部分中应该有一个shipping_info和associated_pin_pad部分,但它们都只出现在最后一个request_status_update中。
或者,如果有人能指出我用 XML PATH 进行这种嵌套的方法,那可能会更简单,我也完全赞成。我只是不知道如何获得一个标题部分,但仍然是多个 request_status_update 部分。XML PATH 似乎希望您总是在逐行的基础上做某事,而不仅仅是一个标题部分。
提前感谢您对此的任何帮助。我知道这是一个很长的代码(对此感到抱歉)
SQL 代码:
SELECT
1 AS Tag
, null AS Parent
, 2 as 'process_document!1!version'
, null as 'header!2!'
, null as 'header!2!sender!Element'
, null as 'header!2!receiver!Element'
, null as 'header!2!creation_dt!Element'
, null as 'request_status_updates!3!'
, null as 'request_status_update!4!request_id!Element'
, null as 'request_status_update!4!status_code_id!Element'
, null as 'request_status_update!4!status_reason_code_id!Element'
, null as 'request_status_update!4!comments!Element'
, null as 'request_status_update!4!serialnumber!Element'
, null as 'associated_pin_pad!5!'
, null as 'associated_pin_pad!5!device_type_id!Element'
, null as 'associated_pin_pad!5!serialnumber!Element'
, null as 'shipping_info!6!'
, null as 'shipping_info!6!shipping_method_id!Element'
, null as 'shipping_info!6!shipping_dt!Element'
, null as 'shipping_info!6!shipping_no!Element'
, null as 'shipping_info!6!shipped_with!Element'
union all
-- header element
select
2 as Tag
, 1 as Parent
, null
, null
, 'SENDER'
, 'RECIPIENT'
, getdate()
, null -- request status updates wrapper tag
, null -- request status update wrapper tag
, null -- request status update element 1
, null -- request status update element 2
, null -- request status update element 3
, null -- request status update element 4
, null -- pinpad wrapper tag
, null -- pinpad element 1
, null -- pinpad element 2
, null -- shipping wrapper tag
, null -- shipping element 1
, null -- shipping element 2
, null -- shipping element 3
, null -- shipping element 4
union all
select
3 as Tag
, 1 as Parent
, null
, null
, null
, null
, null
, null -- request status updates wrapper tag
, null -- request status update wrapper tag
, null -- request status update element 1
, null -- request status update element 2
, null -- request status update element 3
, null -- request status update element 4
, null -- pinpad wrapper tag
, null -- pinpad element 1
, null -- pinpad element 2
, null -- shipping wrapper tag
, null -- shipping element 1
, null -- shipping element 2
, null -- shipping element 3
, null -- shipping element 4
union all
-- request status update element
select
4 as Tag
, 3 as Parent
, null
, null -- header element wrapper tag
, null -- header element 1
, null -- header element 2
, null -- header element 3
, null -- request status updates wrapper tag
, RequestID
, StatusCodeID
, StatusReasonCodeID
, Comment
, SerialNumber
, null -- pinpad wrapper tag
, null -- pinpad element 1
, null -- pinpad element 2
, null -- shipping wrapper tag
, null -- shipping element 1
, null -- shipping element 2
, null -- shipping element 3
, null -- shipping element 4
from dbo.usr_StatusUpdate u
union all
-- Get pinpad information
select
5 as Tag
, 4 as Parent
, null
, null -- header element wrapper tag
, null -- header element 1
, null -- header element 2
, null -- header element 3
, null -- request status updates wrapper tag
, null -- request status update wrapper tag
, null -- request status update element 1
, null -- request status update element 2
, null -- request status update element 3
, null -- request status update element 4
, null -- pinpad wrapper tag
, 'PHRSA' -- placeholder until we get the device type table set
, ltrim(rtrim(dp.CONTSERIALNBR))
, null -- shipping wrapper tag
, null -- shipping element 1
, null -- shipping element 2
, null -- shipping element 3
, null -- shipping element 4
from dbo.usr_StatusUpdate up
inner join dbo.SOP30300 dp on dp.SOPNUMBE = up.SOPNUMBE
where dp.ITEMNMBR like '%PINPAD%'
union all
-- Get shipping information
select
6 as Tag
, 4 as Parent
, null
, null -- header element wrapper tag
, null -- header element 1
, null -- header element 2
, null -- header element 3
, null -- request status updates wrapper tag
, null -- request status update wrapper tag
, null -- request status update element 1
, null -- request status update element 2
, null -- request status update element 3
, null -- request status update element 4
, null -- pinpad wrapper tag
, null -- pinpad element 1
, null -- pinpad element 2
, null -- shipping main container
, ltrim(rtrim(sh.SHIPMTHD)) -- shipping element 1
, sh.ACTLSHIP -- shipping element 2
, null -- shipping element 3
, null -- shipping element 4
from dbo.usr_StatusUpdate sp
inner join dbo.SOP30200 sh on sh.SOPNUMBE = sp.SOPNUMBE
FOR XML explicit
XML 输出:(仅包括 request_status_updates 部分,因为这是问题区域
<process_document version="2">
<header>
<sender>SENDER</sender>
<receiver>RECIPIENT</receiver>
<creation_dt>2013-05-10T10:24:08.593</creation_dt>
</header>
<request_status_updates>
<request_status_update>
<request_id>2016582</request_id>
<status_code_id>CMPLT</status_code_id>
<status_reason_code_id>COMPLT</status_reason_code_id>
<comments>Test request status update interface.</comments>
<serialnumber>123456</serialnumber>
</request_status_update>
<request_status_update>
<request_id>2016583</request_id>
<status_code_id>CMPLT</status_code_id>
<status_reason_code_id>COMPLT</status_reason_code_id>
<comments>Test request status update interface.</comments>
<serialnumber>123457</serialnumber>
<associated_pin_pad>
<device_type_id>PHRSA</device_type_id>
<serialnumber>30500835-AC812195</serialnumber>
</associated_pin_pad>
<associated_pin_pad>
<device_type_id>PHRSA</device_type_id>
<serialnumber>30501048-ac013910</serialnumber>
</associated_pin_pad>
<shipping_info>
<shipping_method_id>OTHER</shipping_method_id>
<shipping_dt>2013-04-24T00:00:00</shipping_dt>
</shipping_info>
<shipping_info>
<shipping_method_id>OTHER</shipping_method_id>
<shipping_dt>2012-08-30T00:00:00</shipping_dt>
</shipping_info>
</request_status_update>
</request_status_updates>
</process_document>
预期输出:
<request_status_updates>
<request_status_update>
<request_id>2016582</request_id>
<status_code_id>CMPLT</status_code_id>
<status_reason_code_id>COMPLT</status_reason_code_id>
<comments>Test request status update interface.</comments>
<serialnumber>123456</serialnumber>
<associated_pin_pad>
<device_type_id>PHRSA</device_type_id>
<serialnumber>30501048-ac013910</serialnumber>
</associated_pin_pad>
<shipping_info>
<shipping_method_id>OTHER</shipping_method_id>
<shipping_dt>2013-04-24T00:00:00</shipping_dt>
</shipping_info>
</request_status_update>
<request_status_update>
<request_id>2016583</request_id>
<status_code_id>CMPLT</status_code_id>
<status_reason_code_id>COMPLT</status_reason_code_id>
<comments>Test request status update interface.</comments>
<serialnumber>123457</serialnumber>
<associated_pin_pad>
<device_type_id>PHRSA</device_type_id>
<serialnumber>30500835-AC812195</serialnumber>
</associated_pin_pad>
<shipping_info>
<shipping_method_id>OTHER</shipping_method_id>
<shipping_dt>2012-08-30T00:00:00</shipping_dt>
</shipping_info>
</request_status_update>
</request_status_updates>
使固定:
我需要添加这个 order by 子句,
order by [request_status_update!4!request_id!Element], Tag
此外,从对标签 4 的查询开始,我需要确保 RequestID 实际上包含在结果中,而不是 NULL,以便排序能够正常工作。
我还将研究使用应该比 XML EXPLICIT 更简单的 XML PATH,但到目前为止,这目前还有效。