SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Unable to get Value from XML


Unable to get Value from XML

Author
Message
bryan 83518
bryan 83518
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 30
<?xml version="1.0" encoding="utf-8"?>
<DataSet xmlns="http://tempuri.org/">
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="KeyID" type="xs:string" minOccurs="0" />
<xs:element name="LicenseAcquisitionUrl" type="xs:string" minOccurs="0" />
<xs:element name="CHECKSUM" type="xs:string" minOccurs="0" />
<xs:element name="ContentID" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<Table diffgr:id="Table1" msdata:rowOrder="0">
<KeyID>ABA38156-D357-4C2A-933A-838604EE4F83</KeyID>
<LicenseAcquisitionUrl>http://ready.steady.com/preauth.aspx?pX=0DF3E4</LicenseAcquisitionUrl>
<CHECKSUM>0A38b6k3kiQ=</CHECKSUM>
<ContentID>zqFuE60mxIQEWEzYyxoMkw==</ContentID>
</Table>
</NewDataSet>
</diffgr:diffgram>
</DataSet>


I have this XML coming from a third party website I am trying to get the KeyID and ContentId values using xquery but it always returns NULL does anybody know how to work with this type of XML.

Bryan
Arthur Olcot
Arthur Olcot
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3610 Visits: 1779
Hi, you can do it by using a namespace. Here is a not so elegant way of getting the value out:

DECLARE @xml XML = '<?xml version="1.0" encoding="utf-8"?>
<DataSet xmlns="http://tempuri.org/">
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="KeyID" type="xs:string" minOccurs="0" />
<xs:element name="LicenseAcquisitionUrl" type="xs:string" minOccurs="0" />
<xs:element name="CHECKSUM" type="xs:string" minOccurs="0" />
<xs:element name="ContentID" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<Table diffgr:id="Table1" msdata:rowOrder="0">
<KeyID>ABA38156-D357-4C2A-933A-838604EE4F83</KeyID>
<LicenseAcquisitionUrl>http://ready.steady.com/preauth.aspx?pX=0DF3E4</LicenseAcquisitionUrl>
<CHECKSUM>0A38b6k3kiQ=</CHECKSUM>
<ContentID>zqFuE60mxIQEWEzYyxoMkw==</ContentID>
</Table>
</NewDataSet>
</diffgr:diffgram>
</DataSet>
'
;
WITH XMLNAMESPACES (DEFAULT 'http://tempuri.org/')
SELECT @xml.value('(//DataSet/*:diffgram/*:NewDataSet/*:Table/*:KeyID/text())[1]', 'UNIQUEIDENTIFIER')
, @xml.value('(//DataSet/*:diffgram/*:NewDataSet/*:Table/*:ContentID/text())[1]', 'VARCHAR(24)')


Eirikur Eiriksson
Eirikur Eiriksson
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40468 Visits: 19463
Slightly different flavour of the same
Cool
DECLARE @TXML XML = '<?xml version="1.0" encoding="utf-8"?>
<DataSet xmlns="http://tempuri.org/">
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="KeyID" type="xs:string" minOccurs="0" />
<xs:element name="LicenseAcquisitionUrl" type="xs:string" minOccurs="0" />
<xs:element name="CHECKSUM" type="xs:string" minOccurs="0" />
<xs:element name="ContentID" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<Table diffgr:id="Table1" msdata:rowOrder="0">
<KeyID>ABA38156-D357-4C2A-933A-838604EE4F83</KeyID>
<LicenseAcquisitionUrl>http://ready.steady.com/preauth.aspx?pX=0DF3E4</LicenseAcquisitionUrl>
<CHECKSUM>0A38b6k3kiQ=</CHECKSUM>
<ContentID>zqFuE60mxIQEWEzYyxoMkw==</ContentID>
</Table>
</NewDataSet>
</diffgr:diffgram>
</DataSet>'


;WITH XMLNAMESPACES (
'urn:schemas-microsoft-com:xml-diffgram-v1' AS diffgr
,DEFAULT 'http://tempuri.org/')
SELECT
DIFF1.GRAM.value('*:KeyID[1]','VARCHAR(50)') AS KeyID
,DIFF1.GRAM.value('*:LicenseAcquisitionUrl[1]','VARCHAR(50)') AS LicenseAcquisitionUrl
,DIFF1.GRAM.value('*:CHECKSUM[1]','VARCHAR(50)') AS [CHECKSUM]
,DIFF1.GRAM.value('*:ContentID[1]','VARCHAR(50)') AS ContentID

FROM @TXML.nodes('DataSet') AS DATAS(ET)
OUTER APPLY DATAS.ET.nodes('diffgr:diffgram/*:NewDataSet/*:Table') AS DIFF1(GRAM)



Results
KeyID                                 LicenseAcquisitionUrl                           CHECKSUM       ContentID
------------------------------------- ----------------------------------------------- -------------- ------------------------
ABA38156-D357-4C2A-933A-838604EE4F83 http://ready.steady.com/preauth.aspx?pX=0DF3E4 0A38b6k3kiQ= zqFuE60mxIQEWEzYyxoMkw==

bryan 83518
bryan 83518
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 30
Thanks everyone both solutions work perfectly

Bryan
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search