对于XML显式嵌套数据不正确

时间:2013-05-10 15:37:36

标签: sql tsql for-xml-path for-xml-explicit

在此之后得到的全部答案 - 如果有下面的情况,它将帮助使用XML显示的其他一些不幸的灵魂:)

我有以下代码输出下面的XML。我遇到的问题是每个 request_status_update 部分都应该有一个 shipping_info associated_pin_pad 部分,但它们都出现了仅在最后 request_status_update

或者,如果有人可以指出我使用XML PATH进行这种嵌套的方法,那可能更简单,我也是为了这一点。我只是无法弄清楚如何获得一个标题部分,但仍然有多个request_status_update部分。 XML PATH似乎期望你总是在逐行的基础上做一些事情而不只是一个标题部分。

提前感谢您提供任何帮助。我知道这是一个很长的代码(对不起)

SQL CODE:

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 OUTPUT :(仅包含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>

FIX:

我需要添加这个order by子句,

order by [request_status_update!4!request_id!Element], Tag

此外,从标记4的查询开始,我需要确保RequestID实际上包含在结果中而不是NULL中,以便排序可以工作。

我还将研究使用XML PATH,它应该比XML EXPLICIT更简单,但到目前为止,它仍在使用。

1 个答案:

答案 0 :(得分:0)

FIX:

我需要添加此order by子句,

order by [request_status_update!4!request_id!Element], Tag

此外,从查询标签4开始,我需要确保结果中实际包含了RequestID而不是NULL,以便可以进行排序。

我还将研究使用XML PATH,它应该比XML EXPLICIT更简单,但到目前为止,它现在仍在起作用。