| Author |
Topic |
|
sqlilliterate
Starting Member
40 Posts |
Posted - 2008-12-31 : 02:52:00
|
Hi All,Help me in obtaining the values from the XML as columns...<Reference> <Basic> <Book>A1.Hj1.JU9</Book> </Basic> <App> <A>AK9.HL9.J0</A> <A>A18.H.PJ69</A> </App> <Sub> <B>B13.H98.P9</B> <B>B18.HO9.JIU8</B> </Sub> <DI> <D>D23.HYT.P6R</D> </DI></Reference> I would like to have the output like...Basic A1.HJ1.JU9App K9.HL9.J0App A18.H.PJ69Sub B13.H98.P9Sub B18.HO9.JIU8DI D23.HYT.P6R Thanks in advance... |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-31 : 03:57:32
|
Here is a technique I've learned from Ken Simmons.-- Setup dummy or sample dataDECLARE @r XMLSET @r = '<Reference> <Basic> <Book>A1.Hj1.JU9</Book> </Basic> <App> <A>AK9.HL9.J0</A> <A>A18.H.PJ69</A> </App> <Sub> <B>B13.H98.P9</B> <B>B18.HO9.JIU8</B> </Sub> <DI> <D>D23.HYT.P6R</D> </DI></Reference>'-- get node structure;WITH Yak (Localname, val, parent)AS (SELECT T.n.value('localname[1]', 'varchar(100)') AS Localname, T.n.value('value[1]', 'VARCHAR(100)') AS val, T.n.value('parent[1]', 'VARCHAR(100)') AS parentFROM ( SELECT x.query(' for $node in /descendant::node()[local-name() != ""] return <node> <localname>{ local-name($node) }</localname> <value>{ $node }</value> <parent>{ local-name($node/..) }</parent> </node>') AS nodes FROM @r.nodes('/') x(x) ) q1 CROSS APPLY q1.nodes.nodes('/node') AS T ( n ))SELECT y2.localname AS nodename, y3.val AS valueFROM Yak AS y1INNER JOIN Yak AS y2 ON y2.parent = y1.localnameINNER JOIN Yak AS y3 ON y3.parent = y2.localname E 12°55'05.63"N 56°04'39.26" |
 |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2008-12-31 : 06:04:28
|
quote: Originally posted by Peso Here is a technique I've learned from Ken Simmons.
I like the CROSS APPLY over the nodes, that's a neat idea, but I don't really see the need for the FLWR expression creating an intermediate document or the CTE and joins for this simple a case.Perhaps I'm missing something, but isn't this sufficient?SELECT T.n.value('local-name(..)[1]', 'varchar(100)') AS nodename, T.n.value('text()[1]', 'varchar(100)') AS valueFROM ( SELECT @r AS ref ) AS Q1CROSS APPLY Q1.ref.nodes('Reference/*/*') AS T ( n ) |
 |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2008-12-31 : 06:16:39
|
quote: Originally posted by Arnold FribbleI like the CROSS APPLY over the nodes, that's a neat idea
...and completely documented in BOL. And entirely unnecessary in my query, which reduces to:SELECT T.n.value('local-name(..)[1]', 'varchar(100)') AS nodename, T.n.value('text()[1]', 'varchar(100)') AS valueFROM @r.nodes('Reference/*/*') AS T ( n )Obviously not back to speed after Christmas! |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-31 : 06:33:49
|
Nice work!Happy New Year to all of you. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-31 : 07:31:10
|
[code]SELECT t.n.value('local-name(..)[1]', 'VARCHAR(100)') AS ParentNodeName, t.n.value('local-name(.)[1]', 'VARCHAR(100)') AS NodeName, t.n.value('text()[1]', 'VARCHAR(100)') AS NodeTextFROM @r.nodes('/*/*/*') AS t(n)[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
sqlilliterate
Starting Member
40 Posts |
Posted - 2009-01-01 : 00:37:46
|
| Thats a great reply !!! Thanks Arnold n Peso.Happy New Year to everyone :) |
 |
|
|
sqlilliterate
Starting Member
40 Posts |
Posted - 2009-01-01 : 07:14:40
|
| Is it possible to form my data which is now in column format back to the same XML format ?Can anyone throw some light on this ? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-01-01 : 13:22:41
|
Huh?What do you mean? E 12°55'05.63"N 56°04'39.26" |
 |
|
|
sqlilliterate
Starting Member
40 Posts |
Posted - 2009-01-02 : 00:04:47
|
| I want to do some calculations on the data obtained in column format... and on processing I would like to frame the column data back into the same XML format.Hope this info is clear... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-02 : 01:12:56
|
| why you want to first take it to columns and then again back to xml? cant you just use modify() method to do calculations inside xml itself? |
 |
|
|
|