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


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


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

Author
Message
suvo.kundu
suvo.kundu
SSC-Enthusiastic
SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)

Group: General Forum Members
Points: 179 Visits: 80
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)
suvo.kundu
suvo.kundu
SSC-Enthusiastic
SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)

Group: General Forum Members
Points: 179 Visits: 80
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


1 address1 NULL
2 address2 NULL
3 address3 kol

Address
city
providerID
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: 3632 Visits: 1779
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
suvo.kundu
suvo.kundu
SSC-Enthusiastic
SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)

Group: General Forum Members
Points: 179 Visits: 80
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

Address address1
Address address2
Address address3
city kol
providerID 1
providerID 2
providerID 3


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
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: 3632 Visits: 1779
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.
suvo.kundu
suvo.kundu
SSC-Enthusiastic
SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)

Group: General Forum Members
Points: 179 Visits: 80
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
suvo.kundu
suvo.kundu
SSC-Enthusiastic
SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)

Group: General Forum Members
Points: 179 Visits: 80
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
1 Address address1
1 Address address2
1 Address address3
2 city kol
3 providerID 1
3 providerID 3
3 providerID 2


now i have to just customize it to
1 address1 NULL
2 address2 NULL
3 address3 kol


please help me to achieve this thing..atleast if not fully generalised
suvo.kundu
suvo.kundu
SSC-Enthusiastic
SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)SSC-Enthusiastic (179 reputation)

Group: General Forum Members
Points: 179 Visits: 80
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
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: 3632 Visits: 1779
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.
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