alex
alex

Reputation: 43

XQuery to get a list of all attributes an element has

Is there a generic way of determining all attributes (and their values) from an XML node using XQuery/XPath?

<parent>
   <something attr1="123" attrA="abc" ..... attrAnythingelse="blablabla"/>
</parent>

Upvotes: 4

Views: 9526

Answers (5)

Александр
Александр

Reputation: 11

DECLARE @xml XML
SET @xml = '<a attr1="5 &lt; 6" attr2="50 &gt; 20"/>'
SELECT
    t.c.value('local-name(.)', 'varchar(128)'),
    t.c.value('.', 'varchar(128)')
FROM
    @xml.nodes('/a/@*') as t(c)

you get

attr_name attr_value
attr1 5 < 6
attr2 50 > 20

Upvotes: 0

Houari Zegai
Houari Zegai

Reputation: 102

Get all attributes with their values using XQuery:

for $attr in //@*
return concat(name($attr), " = &quot;", $attr, "&quot;&#10;")

Upvotes: 0

kadalamittai
kadalamittai

Reputation: 2166

Try this command:

return for $att in $doc//@*
    return (fn:concat(name($att),"=","'",$att,"'"))

Upvotes: 1

Chris Wallace
Chris Wallace

Reputation: 515

$doc//@*/(concat(name(.),"=",.))

Upvotes: 0

Welbog
Welbog

Reputation: 60378

Get all attributes for the current node using XPath:

@*

Is that what you're after?

The names and values of the attributes can be extracted per attribute:

name(@*[1])
string(@*[1])

Depends on what you want to do with them.

Upvotes: 4

Related Questions