2010-07-05 9 views
14

का उपयोग कर मेरे पास नाम:प्राप्त कर रहा है XML तत्व टी एसक्यूएल

<quotes> 
    <quote> 
    <name>john</name> 
    <content>something or other</content> 
    </quote> 
    <quote> 
    <name>mary</name> 
    <content>random stuff</content> 
    </quote> 
</quotes> 

मैं तत्व नाम 'नाम' और 'सामग्री' T-SQL का उपयोग कर की एक सूची कैसे प्राप्त करूं?

सबसे अच्छा मैं अब तक मिल गया है है:

declare @xml xml 
set @xml = ... 
select r.value('quotes/name()[1]', 'nvarchar(20)' as ElementName 
from @xml.nodes('/quotes') as records(r) 

लेकिन, निश्चित रूप से, मैं इस काम करने के लिए नहीं मिल सकता है।

उत्तर

25

वास्तव में, माफ करना, सबसे अच्छा मुझे मिल गया है है:

select distinct r.value('fn:local-name(.)', 'nvarchar(50)') as t 
FROM 
    @xml.nodes('//quotes/*/*') AS records(r) 

लगता है कि मैं अपने ही सवाल का जवाब दे ...

+0

एफवाईआई, समाधान इस पोस्ट में ठोकर से आया: http://stackoverflow.com/questions/2266132/how-can-i-get-a-list-of-element-names-from-an-xml-value-in -एसक्यूएल-सर्वर –

+0

आपका उत्तर ठीक है। आप कुछ उपयोगी XML जिमनास्टिक के लिए इस कॉलम की समीक्षा भी कर सकते हैं: http://beyondrelational.com/blogs/jacob/archive/2010/05/30/select-from-xml.aspx –

4
DECLARE @xml as xml 
SET @xml = '<Address><Home>LINE1</Home></Address>' 

SELECT Nodes.Name.query('local-name(.)') FROM @xml.nodes('//*') As Nodes(Name) 

यह सभी तत्वों की सूची दे देंगे