views:

503

answers:

1

I have an XML like this:

<EXP>
  <TITLES>
    <SUBTITLE CL="AXT4" FL="1" NB="Text 1"/>
  </TITLES>
  <TITLES>
    <SUBTITLE CL="BVT6" FL="2" NB="Text 2"/>
  </TITLES>
  <TITLES>
    <SUBTITLE CL="PLO7" FL="3" NB="Text 3"/>
  </TITLES>
</EXP>

Using XQuery in SQL Server 2008, How can I select Just the value of the attribute NB in a list, by rows (I need all possible values),

Example:

-- Subtitles --

Text 1

Text 2

Text 3

+2  A: 
DECLARE @x xml;
SET @x = N'<EXP>
  <TITLES>
    <SUBTITLE CL="AXT4" FL="1" NB="Text 1"/>
  </TITLES>
  <TITLES>
    <SUBTITLE CL="BVT6" FL="2" NB="Text 2"/>
  </TITLES>
  <TITLES>
    <SUBTITLE CL="PLO7" FL="3" NB="Text 3"/>
  </TITLES>
</EXP>
';

SELECT
    t.c.value(N'@NB', N'nvarchar(10)') AS Subtitles
FROM
    @x.nodes(N'/EXP/TITLES/SUBTITLE') t(c);

The nodes expression shreds into rows, and the value retrieves the column value.

devstuff