Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to parse XML field in SQL Server 2008 R2

Posted on 2014-03-27
5
Medium Priority
?
6,776 Views
Last Modified: 2014-03-28
Hello, I'm trying to parse an XML field in a third party database and I haven't had any luck returning any of the individual elements.  I've been able to parse XML with nested tags before, but apparantly this is too complex.  

Specifically, I need to return all of the RiskCode, Description and Sequence values from the HighRiskIndicators section - but eventually I'll need to return all of the values.

I've attached a sample of the XML below.  Any and all help is greatly appreciated.
XML-Sample.txt
0
Comment
Question by:EasyNiner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 2000 total points
ID: 39960579
Try it this way:
;WITH XMLNAMESPACES ('http://webservices.seisint.com/WsIdentity'  AS WS)

SELECT	T.C.value('WS:RiskCode[1]', 'varchar(10)') RiskCode,	-- Change the data type as appropriate
	T.C.value('WS:Description[1]', 'varchar(100)') [Description],
	T.C.value('WS:Sequence[1]', 'varchar(100)') Sequence
FROM	YourTable
	CROSS APPLY X.nodes('InstantIDModelResponse/WS:Result/WS:Models/WS:Model/WS:Scores/WS:Score/WS:HighRiskIndicators/WS:HighRiskIndicator') T(C)

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39960589
This is how I tested it:
DECLARE @YourTable TABLE (X Xml)
INSERT @YourTable(X) 
VALUES (
'<InstantIDModelResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Header xmlns="http://webservices.seisint.com/WsIdentity">
    <Status>0</Status>
    <QueryId>1234</QueryId>
    <TransactionId>1311111R700000</TransactionId>
  </Header>
  <Result xmlns="http://webservices.seisint.com/WsIdentity">
    <InputEcho>
      <Name>
        <First>TOM</First>
        <Last>SMITH</Last>
      </Name>
      <Address>
        <StreetAddress1>123 Main St</StreetAddress1>
        <City>DESOTO</City>
        <State>TX</State>
        <Zip5>75110</Zip5>
      </Address>
      <DOB>
        <Year>1980</Year>
        <Month>1</Month>
        <Day>15</Day>
      </DOB>
      <Age>34</Age>
      <SSN>123456789</SSN>
      <HomePhone>4697219454</HomePhone>
    </InputEcho>
    <Models>
      <Model>
        <Name>ConsumerVerificationIndex</Name>
        <Scores>
          <Score>
            <Type>nas</Type>
            <Value>8</Value>
            <HighRiskIndicators>
              <HighRiskIndicator>
                <RiskCode>03</RiskCode>
                <Description>The input SSN was issued prior to the input date-of-birth</Description>
                <Sequence>1</Sequence>
              </HighRiskIndicator>
              <HighRiskIndicator>
                <RiskCode>26</RiskCode>
                <Description>Unable to verify SSN/TIN</Description>
                <Sequence>2</Sequence>
              </HighRiskIndicator>
              <HighRiskIndicator>
                <RiskCode>27</RiskCode>
                <Description>Unable to verify phone number</Description>
                <Sequence>3</Sequence>
              </HighRiskIndicator>
              <HighRiskIndicator>
                <RiskCode>52</RiskCode>
                <Description>The input first name is not associated with input SSN</Description>
                <Sequence>4</Sequence>
              </HighRiskIndicator>
              <HighRiskIndicator>
                <RiskCode>10</RiskCode>
                <Description>The input phone number is a mobile number</Description>
                <Sequence>5</Sequence>
              </HighRiskIndicator>
              <HighRiskIndicator>
                <RiskCode>82</RiskCode>
                <Description>The input name and address return a different phone number</Description>
                <Sequence>6</Sequence>
              </HighRiskIndicator>
            </HighRiskIndicators>
          </Score>
        </Scores>
      </Model>
    </Models>
    <DOBVerified>true</DOBVerified>
  </Result>
</InstantIDModelResponse>')

;WITH XMLNAMESPACES ('http://webservices.seisint.com/WsIdentity'  AS WS)

SELECT	T.C.value('WS:RiskCode[1]', 'varchar(10)') RiskCode,		-- Change the data type as appropriate
	T.C.value('WS:Description[1]', 'varchar(100)') [Description],
	T.C.value('WS:Sequence[1]', 'varchar(100)') Sequence
FROM	@YourTable
	CROSS APPLY X.nodes('InstantIDModelResponse/WS:Result/WS:Models/WS:Model/WS:Scores/WS:Score/WS:HighRiskIndicators/WS:HighRiskIndicator') T(C)

Open in new window

Output :
03	The input SSN was issued prior to the input date-of-birth	1
26	Unable to verify SSN/TIN					2
27	Unable to verify phone number					3
52	The input first name is not associated with input SSN		4
10	The input phone number is a mobile number			5
82	The input name and address return a different phone number	6

Open in new window

0
 

Author Closing Comment

by:EasyNiner
ID: 39960666
That was exactly what I was looking for.  I thought I only had to use the namespace alias for the parent tag.  Thanks again!
0
 
LVL 13

Expert Comment

by:Jeff Darling
ID: 39961622
I know this was already solved, but I tried it in MS SQL 2005 without using cross apply and a table.

This may help someone else with a similar issue with XML Namespaces.

DECLARE @myxml as XML;
set @myxml='<InstantIDModelResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Header xmlns="http://webservices.seisint.com/WsIdentity">
    <Status>0</Status>
    <QueryId>1234</QueryId>
    <TransactionId>1311111R700000</TransactionId>
  </Header>
  <Result xmlns="http://webservices.seisint.com/WsIdentity">
    <InputEcho>
      <Name>
        <First>TOM</First>
        <Last>SMITH</Last>
      </Name>
      <Address>
        <StreetAddress1>123 Main St</StreetAddress1>
        <City>DESOTO</City>
        <State>TX</State>
        <Zip5>75110</Zip5>
      </Address>
      <DOB>
        <Year>1980</Year>
        <Month>1</Month>
        <Day>15</Day>
      </DOB>
      <Age>34</Age>
      <SSN>123456789</SSN>
      <HomePhone>4697219454</HomePhone>
    </InputEcho>
    <Models>
      <Model>
        <Name>ConsumerVerificationIndex</Name>
        <Scores>
          <Score>
            <Type>nas</Type>
            <Value>8</Value>
            <HighRiskIndicators>
              <HighRiskIndicator>
                <RiskCode>03</RiskCode>
                <Description>The input SSN was issued prior to the input date-of-birth</Description>
                <Sequence>1</Sequence>
              </HighRiskIndicator>
              <HighRiskIndicator>
                <RiskCode>26</RiskCode>
                <Description>Unable to verify SSN/TIN</Description>
                <Sequence>2</Sequence>
              </HighRiskIndicator>
              <HighRiskIndicator>
                <RiskCode>27</RiskCode>
                <Description>Unable to verify phone number</Description>
                <Sequence>3</Sequence>
              </HighRiskIndicator>
              <HighRiskIndicator>
                <RiskCode>52</RiskCode>
                <Description>The input first name is not associated with input SSN</Description>
                <Sequence>4</Sequence>
              </HighRiskIndicator>
              <HighRiskIndicator>
                <RiskCode>10</RiskCode>
                <Description>The input phone number is a mobile number</Description>
                <Sequence>5</Sequence>
              </HighRiskIndicator>
              <HighRiskIndicator>
                <RiskCode>82</RiskCode>
                <Description>The input name and address return a different phone number</Description>
                <Sequence>6</Sequence>
              </HighRiskIndicator>
            </HighRiskIndicators>
          </Score>
        </Scores>
      </Model>
    </Models>
    <DOBVerified>true</DOBVerified>
  </Result>
</InstantIDModelResponse>';


;WITH XMLNAMESPACES ('http://webservices.seisint.com/WsIdentity'  AS WS)

SELECT
  Nodes.node.value('WS:RiskCode[1]', 'varchar(10)')  RiskCode,
  Nodes.node.value('WS:Description[1]', 'varchar(100)')   [Description],
  Nodes.node.value('WS:Sequence[1]', 'varchar(100)') Sequence
FROM
@myxml.nodes('InstantIDModelResponse/WS:Result/WS:Models/WS:Model/WS:Scores/WS:Score/WS:HighRiskIndicators/WS:HighRiskIndicator') AS Nodes(node);	

Open in new window


MS SQL Query XML with namespaces Example
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39963029
The reason you have to use CROSS APPLY is because you are going against an Xml column ("I'm trying to parse an XML field in a third party database") and more than likely the author will want to select values from the table.

Having said that, if this was a trivial case of querying an Xml variable then yes for sure you are on the right track.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question