how to get the node names as well as node values from xml in sqlserver

  • i have an XML like:-

    <XML>

    <Provider>

    <providerID>1</providerID>

    <Address>address1</Address>

    </Provider>

    <Provider>

    <providerID>2</providerID>

    <Address>address2</Address>

    </Provider>

    here i know the node names .So i could get the all the node values in tabular from.But in ideal situation i will be getting the xml where i will dont know the node names as well as node values.please help me to solve the problem

    alter PROCEDURE spXML

    @id varchar (50)

    as

    Declare @xml xml

    Set @xml =

    '<XML>

    <Provider>

    <providerID>1</providerID>

    <Address>address1</Address>

    </Provider>

    <Provider>

    <providerID>2</providerID>

    <Address>address2</Address>

    </Provider>

    <Provider>

    <providerID>3</providerID>

    <Address>address3</Address>

    <city>kol</city>

    </Provider>

    </XML>'

    Select

    X.T.value('(providerID)[1]', 'int')as providerID,

    X.T.value('(Address)[1]', 'varchar(500)')as Address,

    X.T.value('(city)[1]', 'varchar(500)')as city

    From @xml.nodes('/XML/Provider') as X(T)

  • I could get the node names as column names

    SELECT

    distinct NodeName = C.value('local-name(.)', 'varchar(50)')

    -- , NodeValue = C.value('(.)[1]', 'varchar(50)')

    FROM @xml.nodes('/XML/Provider/*') AS T(C)

    OUTPUT

    1address1NULL

    2address2NULL

    3address3kol

    Address

    city

    providerID

  • I sometimes use this function if I find myself in a similar situation where I need to shred an xml structure to a tabular form but do not know the xml schema.

    http://beyondrelational.com/modules/2/blogs/28/posts/10495/xquery-lab-58-select-from-xml.aspx

  • The output of the query is:-

    Declare @xml xml

    Set @xml =

    '<XML>

    <Provider>

    <providerID>1</providerID>

    <Address>address1</Address>

    </Provider>

    <Provider>

    <providerID>2</providerID>

    <Address>address2</Address>

    </Provider>

    <Provider>

    <providerID>3</providerID>

    <Address>address3</Address>

    <city>kol</city>

    </Provider>

    </XML>'

    SELECT

    distinct

    NodeName = C.value('local-name(.)', 'varchar(50)')

    , NodeValue = C.value('(.)[1]', 'varchar(50)')

    FROM @xml.nodes('/XML/Provider/*') AS T(C)

    output

    Addressaddress1

    Addressaddress2

    Addressaddress3

    citykol

    providerID1

    providerID2

    providerID3

    if any body can help me to change the above out put to this one it can still serve the purpose:-

    ProvideId Address City

    1 address1 NULL

    2 address2 NULL

    3 address3 kol

    please help

  • Try this revised query:

    Declare @xml xml

    Set @xml =

    '<XML>

    <Provider>

    <providerID>1</providerID>

    <Address>address1</Address>

    </Provider>

    <Provider>

    <providerID>2</providerID>

    <Address>address2</Address>

    </Provider>

    <Provider>

    <providerID>3</providerID>

    <Address>address3</Address>

    <city>kol</city>

    </Provider>

    </XML>'

    SELECT t.c.value('providerID[1]', 'int') AS 'ProviderID'

    , t.c.value('Address[1]', 'varchar(100)') AS 'Address'

    , t.c.value('city[1]', 'varchar(100)') AS 'City'

    FROM @xml.nodes('/XML/Provider') AS T(C)

    This outputs it in the format that you have specified.

  • see this is basically for statically getting the tabular structure where i know the node names.

    but for the generalized work around whats the procedure can it be possible?

    I will be getting a random xml based on a particular id

  • if i can manage to get the the starting and the root node as reference...i can make it very generalized like:-

    Declare @xml xml

    Set @xml =

    '<XML>

    <Provider>

    <providerID>1</providerID>

    <Address>address1</Address>

    </Provider>

    <Provider>

    <providerID>2</providerID>

    <Address>address2</Address>

    </Provider>

    <Provider>

    <providerID>3</providerID>

    <Address>address3</Address>

    <city>kol</city>

    </Provider>

    </XML>'

    SELECT

    dense_rank() OVER (ORDER BY C.value('local-name(.)', 'varchar(50)')) AS 'SN',

    NodeName = C.value('local-name(.)', 'varchar(50)')

    , NodeValue = C.value('(.)[1]', 'varchar(50)')

    FROM @xml.nodes('/XML/Provider/*') AS T(C)

    its out put is

    1Addressaddress1

    1Addressaddress2

    1Addressaddress3

    2city kol

    3providerID 1

    3providerID 3

    3providerID 2

    now i have to just customize it to

    1address1NULL

    2address2NULL

    3address3 kol

    please help me to achieve this thing..atleast if not fully generalised

  • i tried to achieve this by creating a table variable and altering its structure with the node name by traversing a loop. and again inserting the node values to the table variable..

    i am finding some problem...in this

    is this the right way to do so..plz advice

    i have deined an udf like

    set ANSI_NULLS ON

    set QUOTED_IDENTIFIER ON

    GO

    ALTER function [dbo].[udf_GetXMLNodeValue_Names](@xml xml)

    returns table

    as

    --Declare @NodeName varchar(100)

    return

    (

    --Declare @xml xml

    --Set @xml =

    --'<XML>

    --<Provider>

    -- <providerID>1</providerID>

    --

    -- <Address>address1</Address>

    --

    --</Provider>

    --

    --<Provider>

    -- <providerID>2</providerID>

    --

    -- <Address>address2</Address>

    --

    --</Provider>

    --<Provider>

    -- <providerID>3</providerID>

    --

    -- <Address>address3</Address>

    -- <city>kol</city>

    --

    --</Provider>

    --

    --</XML>'

    SELECT

    dense_rank() OVER (ORDER BY C.value('local-name(.)', 'varchar(50)')) AS 'SN',

    NodeName = C.value('local-name(.)', 'varchar(50)')

    , NodeValue = C.value('(.)[1]', 'varchar(50)')

    FROM @xml.nodes('/XML/Provider/*') AS T(C)

    )

    my actual stored proc of getting the tabular structure is:-

    set ANSI_NULLS ON

    set QUOTED_IDENTIFIER ON

    go

    alter PROCEDURE [dbo].[spFetchNodeNames_ValuesFromXML]

    AS

    --begin try

    --begin tran

    Declare @xml xml

    Set @xml =

    '<XML>

    <Provider>

    <providerID>1</providerID>

    <Address>address1</Address>

    </Provider>

    <Provider>

    <providerID>2</providerID>

    <Address>address2</Address>

    </Provider>

    <Provider>

    <providerID>3</providerID>

    <Address>address3</Address>

    <city>kol</city>

    </Provider>

    </XML>'

    declare @colName varchar(100);

    declare @Selectsql varchar(100);

    declare @sql varchar(100);

    declare @count int;

    declare @i int;

    set @i=1;

    set @sql='';

    select @count=count(distinct NodeName) from dbo.udf_GetXMLNodeValue_Names (@xml)

    --DECLARE @finaltable TABLE

    --(

    -- SN int identity

    --

    --

    --)

    IF EXISTS (SELECT * FROM sys.tables WHERE name LIKE '#temp%')

    begin

    DROP TABLE #temp

    print ('hi')

    end

    SET @sql = 'CREATE TABLE temptab (SN int )'

    EXEC (@sql)

    WHILE (@i <=@count)

    BEGIN

    select @colName= NodeName from dbo.udf_GetXMLNodeValue_Names (@xml) where SN=@i;

    select(@colName)

    SET @sql = 'ALTER TABLE temptab ADD ' + @colName + ' VARCHAR(100)'

    EXEC (@sql)

    --INSERT INTO temptab VALUES ('COl 1')

    SET @i = @i + 1

    END

    SET @sql = 'ALTER TABLE temptab DROP COLUMN SN'

    EXEC (@sql)

    select * from temptab

    DROP TABLE temptab

    select * from temptab

    --commit

    --end try

    --begin catch

    --rollback

    --end catch

  • Did you have a look at my previous reply regarding the function developed by jacob sebastian? That will shred an xml structure to a flat table similar to what you are trying to do

    http://beyondrelational.com/modules/2/blogs/28/posts/10495/xquery-lab-58-select-from-xml.aspx

    If you truly do not know the xml schema then this function will probably be your best course of action. However, you say that you will know the schema based upon a certain ID? You never mentioned the number of potential schemas, but if fairly low (prob less than about 50), personally I would be inclined to go down the road of creating a view for each schema rather than trying to create something generic that "fits all". In my experience, if you try to create something generic like that, you invariably at some point end up have to cater for a particular schema that doesn't quite fit in which either makes the code quite heavyweight or you end up with lots of conditional statements in there trying to support it. It'll be easier to maintain long term (devs won't be scared picking up the code) and would think better performing as well. However, if you are sure that the schemas are not going to change/get added to then maybe something generic will work better. As always.. it depends. But start off by looking at that link above and trying out the function.

Viewing 9 posts - 1 through 8 (of 8 total)

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