2

I have the following XML and code and can pull back the AccountNumber and Status. I also need to pull back...

  • The ID property of the MFRRequest
  • Several elements from the CaseInformation node

I am not sure how to get the ID property and I'm wondering... can I get all of this information (from multiple nodes) back in the same query?

Thank you!

DECLARE @doc xml
SET @doc =
'<?xml version="1.0" encoding="UTF-8"?>
<p:OrderRequest xmlns:p="http://XXX.Schemas.OrderRequest/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://XXX.Schemas.OrderRequest/2 OrderRequestV2.xsd ">
  <p:Header>
    <p:SCRARequestCount>0</p:SCRARequestCount>
    <p:MFRRequestCount>1</p:MFRRequestCount>
    <p:FileCreatedAt>2001-12-31T12:00:00</p:FileCreatedAt>
    <p:RequestFileName>p:RequestFileName</p:RequestFileName>
  </p:Header>
  <p:RequestItems>
  <p:MFRRequest id="1" priority="">
      <p:AccountNumber>9999999</p:AccountNumber>
      <Status>Initial</Status>
      <p:CaseInformation>
       <ReferralDate>2011-01-01</ReferralDate>
       <LoanType>1A</LoanType>
       <ARM>Yes</ARM>
        <InvestorNumber>InvestorNumber</InvestorNumber>
        <PropertyAddress>PropertyAddress</PropertyAddress>
        <PrivateLabel>Yes</PrivateLabel>
        <CaseNumber>01-11111/AK/</CaseNumber>
        <SuspenseBalance>9999.00</SuspenseBalance>
        <TitleOrderedDate>2011-09-01</TitleOrderedDate>
        <TotalMonthlyPayment>876.99</TotalMonthlyPayment>
      </p:CaseInformation>
    </p:MFRRequest>
  </p:RequestItems>
</p:OrderRequest>'
;WITH XMLNAMESPACES('http://XXX.Schemas.OrderRequest/2' AS p) 

SELECT     
 Y.i.value('Status[1]', 'varchar(10)') AS Status ,
 Y.i.value('p:AccountNumber[1]', 'varchar(10)') AS AccountNumber
FROM      @doc.nodes('/p:OrderRequest/p:RequestItems/p:MFRRequest') AS Y(i) 

1 Answer 1

2

Try something like this:

;WITH XMLNAMESPACES('http://XXX.Schemas.OrderRequest/2' AS p)
SELECT 
    MFRRequestID = Y.i.value('(@id)[1]', 'int'),
    RequestStatus = Y.i.value('Status[1]', 'varchar(10)') ,
    AccountNumber = Y.i.value('p:AccountNumber[1]', 'varchar(10)'),
    ReferralDate = CIF.value('(ReferralDate)[1]', 'varchar(25)'),
    CaseInfoType = CIF.value('(Type)[1]', 'varchar(25)')
FROM 
    @doc.nodes('/p:OrderRequest/p:RequestItems/p:MFRRequest') AS Y(i)
CROSS APPLY
    i.nodes('p:CaseInformation') AS Tbl(CIF)

Basically, once you have the <p:MFRRequest> XML element in your Y(i) pseudo-table, you can then again use a second call to .nodes() to get the (possibly multiple) <p:CaseInfomration> nodes and use .value() calls on this new second pseudo-table to extract single bits of information from it.

Sign up to request clarification or add additional context in comments.

1 Comment

by the way, if you don't want to do all those "p:" prefixes you can specify "with xmlnamespaces(default 'http://...')"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.