苦苦于从XML数据中提取值

时间:2018-03-20 16:47:28

标签: sql-server xml

我一直在努力解决这个问题。谷歌有助于理解“价值”,“节点”,“查询”语法,但出于某种原因,我无法弄清楚如何从我的XML中提取这些数据值。

我已经包含了一些包含查询的示例SQL(标识我想要提取的值)。我已经成功地提取了lsid,但是标签值,例如start_time由于某种原因只是空白。

非常感谢任何帮助...

declare @xmldata xml 
set @xmldata = '<session xmlns="http://www.jumbawumba.com/namespaces/API/reporting" lsid="b59ae352b4xfd48ff8a5y94b5a4235bea7">
  <session_type>support</session_type>
  <lseq>10000</lseq>
  <start_time timestamp="1429447635">2015-04-19T07:47:15-05:00</start_time>
  <end_time timestamp="1429455807">2015-04-19T10:03:27-05:00</end_time>
  <duration>02:16:12</duration>
  <public_site id="1">Default</public_site>
  <external_key />
  <custom_attributes />
  <session_chat_view_url>https://support.jumbawumba.com/session_download?lsid=l%3Db59ae352b4xfd48ff8a5y94b5a4235bea7%3Bh%3D83ecef671cfb95253d95bc3f95d45b193460e2d3%3Bt%3Dsd%3Bm%3Dchat&amp;dl_action=chat&amp;view=1</session_chat_view_url>
  <session_chat_download_url>https://support.jumbawumba.com/session_download?lsid=l%3Db59ae352b4xfd48ff8a5y94b5a4235bea7%3Bh%3D83ecef671cfb95253d95bc3f95d45b193460e2d3%3Bt%3Dsd%3Bm%3Dchat&amp;dl_action=chat</session_chat_download_url>
  <session_recording_view_url>https://support.jumbawumba.com/session_download?lsid=l%3Db59ae352b4xfd48ff8a5y94b5a4235bea7%3Bh%3D76417a36304931487d8feaba2810e5dbdb21c96b%3Bt%3Dsd%3Bm%3Drecording&amp;dl_action=recording&amp;view=1</session_recording_view_url>
  <session_recording_download_url>https://support.jumbawumba.com/session_download?lsid=l%3Db59ae352b4xfd48ff8a5y94b5a4235bea7%3Bh%3D76417a36304931487d8feaba2810e5dbdb21c96b%3Bt%3Dsd%3Bm%3Drecording&amp;dl_action=recording</session_recording_download_url>
  <file_transfer_count>0</file_transfer_count>
  <file_move_count>0</file_move_count>
  <file_delete_count>0</file_delete_count>
  <primary_customer gsnumber="2303">[Pinned] J4RTHE345</primary_customer>
  <primary_rep gsnumber="2301" id="202">David Hasselhof - Jumbawumba</primary_rep>
</session>'

select @xmldata 

select

    lsid = @xmldata.value('(/*/@lsid)[1]', 'VARCHAR(500)')
    ,start_time = @xmldata.query('/session/start_time')  --This doesn't work
    ,end_time = null 
    ,duration = null
    ,file_transfer_count = null
    ,file_move_count = null
    ,file_delete_count = null
    ,primary_customer = null
    ,primary_rep = null
    ,public_ip = null
    ,private_ip = null
    ,hostname = null
    ,@xmldata

1 个答案:

答案 0 :(得分:0)

这会返回您的值:

select

    lsid = @xmldata.value('(/*/@lsid)[1]', 'VARCHAR(500)')
    ,start_time = @xmldata.value('(/*:session/*:start_time)[1]', 'VARCHAR(50)')  --works
    ,end_time = null 
    ,duration = null
    ,file_transfer_count = null
    ,file_move_count = null
    ,file_delete_count = null
    ,primary_customer = null
    ,primary_rep = null
    ,public_ip = null
    ,private_ip = null
    ,hostname = null
    ,@xmldata