Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,

I need to parse the below xml in sql server to get the value of the element "dc.01.02:SC.PC.Amount".

XML
<xbrl xmlns:dc.01.02="http://www.google.com">
    <dc.01.02:SC.SG.Amount decimals="2" contextRef="SFM" unitRef="AUD">-500</dc.01.02:SC.SG.Amount>
    <dc.01.02:SC.AP.Amount decimals="2" contextRef="SFM" unitRef="AUD">10</dc.01.02:SC.AP.Amount>
    <dc.01.02:SC.PC.Amount decimals="2" contextRef="SFM" unitRef="AUD">20</dc.01.02:SC.PC.Amount>
    <dc.01.02:SC.SS.Amount decimals="2" contextRef="SFM" unitRef="AUD">30</dc.01.02:SC.SS.Amount>
    <dc.01.02:SC.CV.Amount decimals="2" contextRef="SFM" unitRef="AUD">40</dc.01.02:SC.CV.Amount>
    <dc.01.02:SC.SC.Amount decimals="2" contextRef="SFM" unitRef="AUD">50</dc.01.02:SC.SC.Amount>
    <dc.01.02:SC.CC.Amount decimals="2" contextRef="SFM" unitRef="AUD">60</dc.01.02:SC.CC.Amount>
    <dc.01.02:SC.OC.Amount decimals="2" contextRef="SFM" unitRef="AUD">70</dc.01.02:SC.OC.Amount>
</xbrl>


-----

so my output look like "20"

Please help me out here in SQL

Thanks & Regards
chiru
Posted
Updated 18-Sep-14 21:00pm
v2
Comments
Herman<T>.Instance 19-Sep-14 3:19am    
a benefit of SQL Server 2014: http://msdn.microsoft.com/en-us/library/ms190798.aspx
don't know how it works on 2012.

1 solution

Try this approach

SQL
DECLARE @string NVARCHAR(MAX);
SET @string = 
  '<xbrl xmlns:dc.01.02="http://www.google.com">
    <dc.01.02:sc.sg.amount decimals="2" contextref="SFM" unitref="AUD">-500</dc.01.02:sc.sg.amount>
    <dc.01.02:sc.ap.amount decimals="2" contextref="SFM" unitref="AUD">10</dc.01.02:sc.ap.amount>
    <dc.01.02:sc.pc.amount decimals="2" contextref="SFM" unitref="AUD">20</dc.01.02:sc.pc.amount>
    <dc.01.02:sc.ss.amount decimals="2" contextref="SFM" unitref="AUD">30</dc.01.02:sc.ss.amount>
    <dc.01.02:sc.cv.amount decimals="2" contextref="SFM" unitref="AUD">40</dc.01.02:sc.cv.amount>
    <dc.01.02:sc.sc.amount decimals="2" contextref="SFM" unitref="AUD">50</dc.01.02:sc.sc.amount>
    <dc.01.02:sc.cc.amount decimals="2" contextref="SFM" unitref="AUD">60</dc.01.02:sc.cc.amount>
    <dc.01.02:sc.oc.amount decimals="2" contextref="SFM" unitref="AUD">70</dc.01.02:sc.oc.amount>
</xbrl>'

SET @string = REPLACE(@string, ':SC.', '_SC.')

DECLARE @XMLSTRING XML = @string

SELECT  T.C.query('node()')
from	@XMLSTRING.nodes('/xbrl/dc.01.02_SC.PC.Amount') as T(C)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900