XML issue

  • Can anybody please help me with the XML below. I need to extract Initials,Gender and Phone info:

    DECLARE @XmlTable TABLE (ID INT NOT NULL, XMLDATA XML)

    INSERT INTO @XmlTable VALUES(1,

    '<updateProfilesRequest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <updateProfile xmlns="http://www.fictivewebsite.nl/ops/updateProfiles/v1.0">

    <fullRelation>

    <initials>J. M. C.</initials>

    <lastName>Fictive_last_name</lastName>

    <gender>Female</gender>

    <phoneNumber>

    <areaCode>0184</areaCode>

    <number>617896</number>

    </phoneNumber>

    </fullRelation>

    </updateProfile>

    </updateProfilesRequest>')

    ;WITH XMLNAMESPACES('http://www.fictivewebsite.nl/ops/updateProfiles/v1.0' AS ns1)

    SELECT

    Initials = XmlData.value('(updateProfilesRequest/updateProfile/ns1:fullRelation/ns1:initials)[1]', 'varchar(25)'),

    Gender = XmlData.value('(updateProfilesRequest/updateProfile/ns1:fullRelation/ns1:gender)[1]', 'varchar(25)'),

    PhoneArea = XmlData.value('(updateProfilesRequest/updateProfile/ns1:fullRelation/ns1:phonenumber/ns1:areacode)[1]', 'varchar(25)'),

    PhoneNo = XmlData.value('(updateProfilesRequest/updateProfile/ns1:fullRelation/ns1:phonenumber/ns1:number)[1]', 'varchar(25)')

    FROM

    @XmlTable

  • This works:

    DECLARE @XmlTable TABLE (ID INT NOT NULL, XMLDATA XML)

    INSERT INTO @XmlTable VALUES(1,

    '<updateProfilesRequest xmlns="http://www.fictivewebsite.nl/ops/updateProfiles/v1.0">

    <updateProfile >

    <fullRelation>

    <initials>J. M. C.</initials>

    <lastName>Fictive_last_name</lastName>

    <gender>Female</gender>

    <phoneNumber>

    <areaCode>0184</areaCode>

    <number>617896</number>

    </phoneNumber>

    </fullRelation>

    </updateProfile>

    </updateProfilesRequest>');

    WITH XMLNAMESPACES(DEFAULT 'http://www.fictivewebsite.nl/ops/updateProfiles/v1.0')

    SELECT

    Initials = XmlData.value('(/updateProfilesRequest/updateProfile/fullRelation/initials)[1]', 'varchar(25)'),

    Gender = XmlData.value('(/updateProfilesRequest/updateProfile/fullRelation/gender)[1]', 'varchar(25)'),

    PhoneArea = XmlData.value('(/updateProfilesRequest/updateProfile/fullRelation/phoneNumber/areaCode)[1]', 'varchar(25)'),

    PhoneNo = XmlData.value('(/updateProfilesRequest/updateProfile/fullRelation/phoneNumber/number)[1]', 'varchar(25)')

    FROM @XmlTable X

    Gerald Britton, Pluralsight courses

  • Many thanks !

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply