1

getting Null value for xml cast in parsing a element "ErrorType"

I have this XML stored in my SQL database column.

<Response>
    <ISSuccessfulPingResponse>false</ISSuccessfulPingResponse>
    <PingResponse>
        <PriceResult xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://www.test.com/pasd/test.xsd">
            <Result>ERROR</Result>
            <ErrorType>XML Validation</ErrorType>
            <ErrorDescription> equal to 5.</ErrorDescription>
            <out>0</out>
        </PriceResult>
    </PingResponse>
</Response>

and I tried to run an SQL script for getting value for element "ErrorType", but I am receiving null value.

SELECT  Cast(Result as xml).value ('(/Response/PingResponse/PriceResult/ErrorType)[1]', 'varchar(100)' ) as result  from  getresults
where Id=23

1 Answer 1

2

Since PriceResult and all of the descendant elements without prefix (including ErrorType) are in the namespace https://www.test.com/pasd/test.xsd, so you should be able to use WITH XMLNAMESPACES here, for example :

;WITH XMLNAMESPACES('https://www.test.com/pasd/test.xsd' as d)
SELECT Cast(Result as xml).value ('
    (/Response/PingResponse/d:PriceResult/d:ErrorType)[1]
', 'varchar(100)' ) as result 
from getresults where Id=23

Basically, the codes defined a prefix d that mapped to the namespace URI using WITH XMLNAMESPACES, and then use it (prefix d) to reference elements in the namespace.

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.