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
 General SQL Server Forums
 New to SQL Server Programming
 Extract a value from xml

Author  Topic 

Yanivbu
Starting Member

2 Posts

Posted - 2015-02-17 : 01:33:53
Hi,
I have the following xml:
<MachineRepostitoryDS xmlns="http://Testing.com/Deployment/MachineRepostitoryDS.xsd">
<Components>
<Environment>QA400</Environment>
<HostName>HostTesting</HostName>
<Component>EventProcessorService</Component>
<CompStatus>OK. 17 Testing assemblies found</CompStatus>
<Version>4.0.24.0</Version>
<Path>D:\Testing\EventProcessor.WinService</Path>
</Components>

I would like to extract the version value from it (in the case above it's- 4.0.24.0).
Is there a way to do it?

Thanks,

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2015-02-17 : 01:41:43
If you'd like to shred xml with SQL Server - follow these instructions\example - http://www.sqlserver-dba.com/2013/05/shredding-xml-with-powershell-and-sql-server.html
It's a common problem to extract xml and place into EAV tables

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page

Yanivbu
Starting Member

2 Posts

Posted - 2015-02-17 : 04:46:58
Thank you- I will try it.

quote:
Originally posted by jackv

If you'd like to shred xml with SQL Server - follow these instructions\example - http://www.sqlserver-dba.com/2013/05/shredding-xml-with-powershell-and-sql-server.html
It's a common problem to extract xml and place into EAV tables

Jack Vamvas
--------------------
http://www.sqlserver-dba.com


Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-02-17 : 09:04:16
After fixing you XML:


declare @xml xml =
'<MachineRepostitoryDS xmlns="http://Testing.com/Deployment/MachineRepostitoryDS.xsd">
<Components>
<Environment>QA400</Environment>
<HostName>HostTesting</HostName>
<Component>EventProcessorService</Component>
<CompStatus>OK. 17 Testing assemblies found</CompStatus>
<Version>4.0.24.0</Version>
<Path>D:\Testing\EventProcessor.WinService</Path>
</Components>
</MachineRepostitoryDS>'
;
with XMLNAMESPACES (default 'http://Testing.com/Deployment/MachineRepostitoryDS.xsd')
select n.value('Version[1]', 'nvarchar(20)')
from @xml.nodes('//Components') x(n)
Go to Top of Page
   

- Advertisement -