XML列提取![CDATA []]

时间:2011-08-04 12:54:45

标签: xml sql-server-2008 cdata

我运行以下查询从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 个答案:

答案 0 :(得分:4)

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