简体   繁体   中英

How to ignore xml namespaces of root and child nodes and query the value with comma separated value from xml in SQL

Here i've a xml in a table 'Test' as below

Id     Input
------------------------------------------------------
1      <DeviceList xmlns="www.domain.com/devicelist">
         <Device xmlns="" Name="Device1">1</Device>
         <Device xmlns="" Name="Device2">2</Device>
       </DeviceList>

2      <DeviceList xmlns="www.domain.com/devicelist">
         <Device xmlns="" Name="Device3">3</Device>
         <Device xmlns="" Name="Device4">4</Device>
       </DeviceList>

3      <DeviceList>
         <Device>4</Device>
         <Device>5</Device>
       </DeviceList>

my expected result would be as below,

Id   DeviceIds
--------------
1    1,2
2    3,4
3    4,5

My query,

SELECT Id, 
STUFF((SELECT 
', ' + CAST(Id.query('./text()') as VARCHAR(MAX)) FROM Input.nodes('/DeviceList/Device') AS Projectors(Id) FOR XML PATH('')), 1, 1, '') AS DeviceIds
FROM test;

But, this query return below result,

Id  DeviceIds
-------------
1   NULL
2   NULL
3   4,5

Anybody help/suggest me on this.

Judging by the following question query XML while ignoring namespace?

It looks like /*:tagname might help.

If that does not work this question also provided another work around Ignore XML namespace in T-SQL

But it would require a little knowledge before of the namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM