Get the names of attributes from an element in a SQL XML column
Asked Answered
P

4

8

For this xml (in a SQL 2005 XML column):

<doc>
 <a>1</a>
 <b ba="1" bb="2" bc="3" />
 <c bd="3"/>
</doc>

I'd like to be able to retrieve the names of the attributes (ba, bb, bc, bd) rather than the values inside SQL Server 2005. Well, XPath certainly allows this with name() but SQL doesn't support that. This is my chief complaint with using XML in SQL; you have to figure out which parts of the XML/Xpath/XQuery spec are in there.

The only way I can think of to do this is to build a CLR proc that loads the XML into an XML Document (iirc) and runs the XPath to extract the names of the nodes. I'm open to suggestions here.

Picrite answered 27/10, 2008 at 21:11 Comment(1)
Wow this got an upvote 7.5 years after I asked it.Picrite
T
5
DECLARE @xml as xml
DECLARE @path as varchar(max)
DECLARE @index int, @count int

SET @xml = 
'<doc>
 <a>1</a>
 <b ba="1" bb="2" bc="3" />
 <c bd="3"/>
</doc>'



SELECT @index = 1

SET @count = @xml.query('count(/doc/b/@*)').value('.','int')

WHILE @index <= @count 
BEGIN
    SELECT  @xml.value('local-name((/doc/b/@*[sql:variable("@index")])[1])', 'varchar(max)')
    SET @index = @index + 1
END

for element 'b'

it returns

  • ba
  • bb
  • bc

You can build a loop to get attributes for each element in the xml.

BTW The XML in your sample should be closed at closing doc tag.

Taxonomy answered 27/10, 2008 at 23:12 Comment(0)
H
9
DECLARE @xml as xml

SET @xml = 
'<doc>
 <a>1</a>
 <b ba="1" bb="2" bc="3" />
 <c bd="3"/>
</doc>'

SELECT DISTINCT
 CAST(Attribute.Name.query('local-name(.)') AS VARCHAR(100)) Attribute,
 Attribute.Name.value('.','VARCHAR(100)') Value
FROM @xml.nodes('//@*') Attribute(Name)

Returns:

Attribute Value

ba 1

bb 2

bc 3

bd 3

Hosiery answered 11/11, 2010 at 16:19 Comment(0)
T
5
DECLARE @xml as xml
DECLARE @path as varchar(max)
DECLARE @index int, @count int

SET @xml = 
'<doc>
 <a>1</a>
 <b ba="1" bb="2" bc="3" />
 <c bd="3"/>
</doc>'



SELECT @index = 1

SET @count = @xml.query('count(/doc/b/@*)').value('.','int')

WHILE @index <= @count 
BEGIN
    SELECT  @xml.value('local-name((/doc/b/@*[sql:variable("@index")])[1])', 'varchar(max)')
    SET @index = @index + 1
END

for element 'b'

it returns

  • ba
  • bb
  • bc

You can build a loop to get attributes for each element in the xml.

BTW The XML in your sample should be closed at closing doc tag.

Taxonomy answered 27/10, 2008 at 23:12 Comment(0)
S
3

this:

declare @xml as xml

set @xml = 
'<doc>
 <a>1</a>
 <b ba="1" bb="2" bc="3" />
 <c bd="3"/>
</doc>'

select @xml.query('
    for $attr in /doc/b/@*
    return local-name($attr)') 

returns:

ba bb bc

Speechmaker answered 3/11, 2009 at 12:43 Comment(0)
T
1
Declare @xml Xml = '<doc><a>1</a><b ba="1" bb="2" bc="3" /><c bd="3"/></doc>'

Select n.value('local-name(.)', 'varchar(max)')  from @xml.nodes('/doc/*/@*') a(n)

Returns ba bb bc bd

Tatianna answered 7/9, 2018 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.