0

I'm trying to parse the XML data coming from the SQL Server. But although I tried many ways, I could not succeed.

When I run the script it does not give an error but returns a not record

DECLARE @xmlData XML

SET @xmlData = '<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Body>
    <S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">
      <faultcode>S:VersionMismatch</faultcode>
      <faultstring>Couldnt create SOAP message. Expecting Envelope in namespace http://schemas.xmlsoap.org/soap/envelope/, but got null </faultstring>
    </S:Fault>
  </S:Body>
</S:Envelope>
'

SET @xmlData = (SELECT @xmlData.query('declare default element namespace "http://schemas.xmlsoap.org/soap/envelope/";
/Envelope/Body'))

SELECT @xmlData

SELECT b.value('(./Fault/faultcode/text())[1]', 'Varchar(50)') AS [Name]
  FROM @xmlData.nodes('/Body') AS a (b)

1 Answer 1

0

Using WITH XMLNAMESPACES:

;WITH XMLNAMESPACES('http://schemas.xmlsoap.org/soap/envelope/' AS S)
SELECT b.value('(./S:Fault/faultcode/text())[1]', 'Varchar(50)') AS [Name]
  FROM @xmlData.nodes('//S:Body') AS a (b);

db<>fiddle demo

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

Comments

Your Answer

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