Извлечение столбца XML! [CDATA []] - PullRequest
1 голос
/ 04 августа 2011

Я выполняю приведенный ниже запрос для извлечения данных из столбца xml ... вопрос в том, как извлечь данные, которые находятся между ними! [CDATA [""]]?

select        
     CAST(xml as xml).value('(//@nodeName)[1]','nvarchar(20)') as NodeName,       
     CAST(xml as xml).value('(//![CDATA [prdDetDesc]])[1]','nvarchar(225)') as DetDesc,
     CAST(xml as xml).value('(//prdImg)[1]','nvarchar(1000)') as prdImage
from [dbo].[cmsContentXml])

Мне нужноизвлечь данные, которые находятся между [[""]]

Заранее спасибо

1 Ответ

4 голосов
/ 04 августа 2011

В разделах CDATA нет ничего особенного.

declare @xml xml = 
'<productDetailsDescription>
   <![CDATA[Candidates for this exam are professionals who typically pursue careers as database administrators, database developers, or business intelligence developers. Additionally, they can be people who do not work with Microsoft SQL Server as a part of their primary job functions but who want to show their breadth of technology experience, such as developers, systems administrators, and others. ]]>
 </productDetailsDescription>
 <productImage>/m/967558/40.jpg</productImage>
 <application>Slmnoasp</application>'

select @xml.value('/productDetailsDescription[1]', 'nvarchar(225)')

Он также обрабатывает смешанные значения.

declare @xml xml = 
'<root>123<![CDATA[ABD]]>456</root>'  

select @xml.value('/root[1]', 'nvarchar(10)')

Результат:

(No column name)
123ABD456

Редактировать

Из таблицы вместо переменной с приведением к XML:

select cast(xml as xml).value('/productDetailsDescription[1]', 'nvarchar(max)') as productDetailsDescription
from YourTable

Попробуйте здесь: http://data.stackexchange.com/stackoverflow/q/108293/

Редактировать 2

Вам необходимо указать имена узлов в запросе. Вы также должны решить, должны ли вы иметь разные узлы в одном и том же столбце или они должны быть в разных столбцах. Ниже я покажу вам, как вы можете сделать оба.

declare @T table(xml nvarchar(max))

insert into @T values
('<productDetailsDescription>
   <![CDATA[Candidates for this exam are professionals who typically pursue careers as database administrators, database developers, or business intelligence developers. Additionally, they can be people who do not work with Microsoft SQL Server as a part of their primary job functions but who want to show their breadth of technology experience, such as developers, systems administrators, and others. ]]>
 </productDetailsDescription>
 <productImage>/m/967558/40.jpg</productImage>
 <application>Slmnoasp</application>')

insert into @T values
('<detailDescription>
   <![CDATA[Candidates for this exam are professionals who typically pursue careers as database administrators, database developers, or business intelligence developers. Additionally, they can be people who do not work with Microsoft SQL Server as a part of their primary job functions but who want to show their breadth of technology experience, such as developers, systems administrators, and others. ]]>
 </detailDescription>
 <productImage>/m/967558/40.jpg</productImage>
 <application>Slmnoasp</application>')

-- Get detailDescription in a column of its own
select 
  cast(xml as xml).value('/productDetailsDescription[1]', 'nvarchar(max)') as productDetailsDescription,
  cast(xml as xml).value('/detailDescription[1]', 'nvarchar(max)') as detailDescription
from @T

-- Get detailDescription in the same column as productDetailsDescription
select 
  cast(xml as xml).value('/*[local-name()=("productDetailsDescription","detailDescription")][1]', 'nvarchar(max)') as detailDescription
from @T
...