Solved

How to parse XML field in SQL Server 2008 R2

Posted on 2014-03-27
5
6,406 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
  • 3
5 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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 12

Expert Comment

by:Jeff Darling
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now