简体   繁体   中英

SQL Server XML query: how to select values?

How to select file names from xml like this, ie I need only names:

c:\temp\f1.txt
c:\temp\f2.txt

XML like this:

<root name="name" id="12">
<class1>
     <file name="c:\temp\f1.txt">
</class1>
<class1>
   <subclass1>
        <file name="c:\temp\f2.txt">
    </subclass1>
</class1>
</root>
declare @XML xml 
set @XML = '
<root name="name" id="12">
  <class1>
       <file name="c:\temp\f1.txt"/>
  </class1>
  <class1>
     <subclass1>
          <file name="c:\temp\f2.txt"/>
      </subclass1>
  </class1>
</root>'

select T.N.value('@name', 'nvarchar(100)') as FileName
from @XML.nodes('//file') as T(N)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM