Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Obtain values from XML

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.JU9
App K9.HL9.J0
App A18.H.PJ69
Sub B13.H98.P9
Sub B18.HO9.JIU8
DI 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 data
DECLARE @r XML

SET @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 parent
FROM ( 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 value
FROM Yak AS y1
INNER JOIN Yak AS y2 ON y2.parent = y1.localname
INNER JOIN Yak AS y3 ON y3.parent = y2.localname



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

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 value
FROM ( SELECT @r AS ref ) AS Q1
CROSS APPLY Q1.ref.nodes('Reference/*/*') AS T ( n )

Go to Top of Page

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2008-12-31 : 06:16:39
quote:
Originally posted by Arnold Fribble
I 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 value
FROM @r.nodes('Reference/*/*') AS T ( n )


Obviously not back to speed after Christmas!
Go to Top of Page

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"
Go to Top of Page

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 NodeText
FROM @r.nodes('/*/*/*') AS t(n)[/code]


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

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 :)
Go to Top of Page

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 ?
Go to Top of Page

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"
Go to Top of Page

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...
Go to Top of Page

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?
Go to Top of Page
   

- Advertisement -