0

I am trying to parser below XML with the mentioned code. But results are blank.

DECLARE @XmlFile XML

SELECT @XmlFile = BulkColumn AS XML 
FROM OPENROWSET(BULK 'C:\GET_QUOTA_DATA_resTEST.xml', SINGLE_BLOB) x

SELECT 
BucketBucket_id = bucket.value('(bucket_id)[1]', 'varchar(255)'),
BucketName = bucket.value('(name)[1]', 'varchar(255)')
FROM
@XmlFile.nodes('/bucket') AS XTbl1(bucket)

XML is as below

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:toa:capacity">
       <SOAP-ENV:Body>
          <ns1:get_quota_data_response>
            <bucket>
             <bucket_id>FT_10503_DP1019</bucket_id>
             <name>ABC Co</name>
                <day>
                    <date>2015-08-21</date>
                </day>
           </bucket>
      </ns1:get_quota_data_response>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
2
  • Is that all XML? It is not correctly formed. Try SELECT @XmlFile/PRINT @XmlFile. Describe also desired result. Commented Aug 23, 2015 at 1:53
  • @lad2025 It's fine above I haven't put the whole XML may be that's why. Updated now. Commented Aug 23, 2015 at 2:02

1 Answer 1

1

Try this:

 SELECT 
     BucketBucket_id = bucket.value('(bucket_id)[1]', 'varchar(255)'),
     BucketName = bucket.value('(name)[1]', 'varchar(255)')
 FROM 
     @XmlFile.nodes('//bucket') AS XTbl1(bucket)               

You used /bucket which is direct path from root. That's why it didn't work.

  • / will define an absolute path to node "a" relative to the root
  • // will define a path to node "a" anywhere within the XML document
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent // made the difference.

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.