Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

how to get the node names as well as node values from xml in sqlserver Expand / Collapse
Author
Message
Posted Thursday, September 6, 2012 1:31 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, January 12, 2013 8:31 AM
Points: 17, 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)






Post #1355591
Posted Thursday, September 6, 2012 1:57 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, January 12, 2013 8:31 AM
Points: 17, 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
Post #1355603
Posted Friday, September 7, 2012 1:18 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 5:10 AM
Points: 2,587, Visits: 1,626
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
Post #1355787
Posted Sunday, September 9, 2012 4:38 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, January 12, 2013 8:31 AM
Points: 17, 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
Post #1356458
Posted Sunday, September 9, 2012 6:34 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 5:10 AM
Points: 2,587, Visits: 1,626
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.
Post #1356465
Posted Sunday, September 9, 2012 6:53 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, January 12, 2013 8:31 AM
Points: 17, 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

Post #1356466
Posted Sunday, September 9, 2012 7:00 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, January 12, 2013 8:31 AM
Points: 17, 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

Post #1356467
Posted Sunday, September 9, 2012 7:27 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, January 12, 2013 8:31 AM
Points: 17, 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
Post #1356473
Posted Sunday, September 9, 2012 7:33 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 5:10 AM
Points: 2,587, Visits: 1,626
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.
Post #1356475
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse