SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


XML hierarchical data


XML hierarchical data

Author
Message
alexandru.tero.toth
alexandru.tero.toth
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 78
hello

I have a request to collect some data from an xml file, and I am stuck at this point. The xml is structured like this

[...]
<device id ='...'>
.....
<sensor id = '...'>
........
</sensor>
.....
<sensor id = '...'>
........
</sensor>
.....
<sensor id = '...'>
........
</sensor>
...
</device>

I want to generate a table that contains the device id and sensor id like this:

deviceID sensorID
40 256
40 258
40 259
51 1011
51 1012
etc etc

can you please help me out with an idea.


thank you
Alex
Mark Cowne
Mark Cowne
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14679 Visits: 26568
Something like this?



DECLARE @x XML = '
<Root>
<device id ="40">
<sensor id = "256">
</sensor>
<sensor id = "258">
</sensor>
<sensor id = "259">
</sensor>

</device>
<device id ="51">
<sensor id = "1011">
</sensor>
<sensor id = "1012">
</sensor>
</device>
</Root>
'

SELECT x.device.value('@id[1]','INT') AS deviceID,
y.sensor.value('@id[1]','INT') AS sensorID
FROM @x.nodes('/Root/device') AS x(device)
OUTER APPLY x.device.nodes('sensor') AS y(sensor)



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




alexandru.tero.toth
alexandru.tero.toth
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 78
Thank you Mark
Alan Burstein
Alan Burstein
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32297 Visits: 8574
Because you are dealing with a one-to-many parent/child node relationship you could start your query with /Root/device/sensor as your context like so:

(Taking the example code that Mark was nice enough to put together)

SELECT x.device.value('(../@id)[1]','INT') AS deviceID,
x.device.value('(@id)[1]','INT') AS sensorID
FROM @x.nodes('/Root/device/sensor') x(device)



Here we don't need to do any joins (or use apply). The downside is that this produces a more complex and slightly slower query plan. That said, I have been using sp_xml_preparedocument lately which performs wonderfully and with a simple query plan:


DECLARE @x XML = '
<Root>
<device id ="40">
<sensor id = "256">
</sensor>
<sensor id = "258">
</sensor>
<sensor id = "259">
</sensor>

</device>
<device id ="51">
<sensor id = "1011">
</sensor>
<sensor id = "1012">
</sensor>
</device>
</Root>
'

DECLARE @hdoc int

EXEC sp_xml_preparedocument @hdoc OUTPUT, @x

select *
from OPENXML (@hdoc, '/Root/device/sensor', 2)
WITH (
deviceID int '../@id',
sensorID int '@id'
)



Edit: Added SQL Code -- e.g. (code="sql")

-- Alan Burstein


Helpful links:

Best practices for getting help on SQLServerCentral -- Jeff Moden
How to Post Performance Problems -- Gail Shaw

Nasty fast set-based string manipulation functions:
For splitting strings try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL Server 2012+)
To split strings based on patterns try PatternSplitCM
Need to clean or transform a string? try NGrams, PatExclude8K, PatReplace8K, DigitsOnlyEE, or Translate8K

I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. -- Itzik Ben-Gan 2001

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search