使用名称空间SQL解析XML

问题描述:

我们正在清理数据库中的数据,并且列中包含XML详细信息,我们希望它能够转换为纯文本。使用名称空间SQL解析XML

下面是表格列中的示例XML。

<FlowDocument PagePadding="5,5,5,5" Name="RTDocument" AllowDrop="True" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> 
    <Paragraph>FILE DESTROYED - MAY 21st, 2008</Paragraph> 
    <Paragraph>todo</Paragraph> 
</FlowDocument> 

我使用此查询,但不渲染所需的输出,由于命名空间的存在(如果我删除从XML命名空间,我能够成功地渲染输出)。

SELECT 
    CAST(CAST(Comments AS XML).query('data(/FlowDocument/Paragraph)') AS VARCHAR(7000)) AS activity 
FROM 
    dbo.Activities 
WHERE 
    ActivityID = 1 

请在这件事上帮助。

感谢

您需要使用命名空间声明在您的查询按:https://msdn.microsoft.com/en-us/library/ms191474.aspx

所以你的查询部分看起来是这样的:

query(' 
declare namespace NS="http://schemas.microsoft.com/winfx/2006/xaml/presentation"; 
data(/NS:FlowDocument/NS:Paragraph) 
') 
+0

非常感谢。它工作完美 –

+0

非常欢迎您。请考虑接受答案。 – Anton

+0

@SidharthSoneja如果这回答您的问题到您满意,您应该检查答案旁边的✔。阅读更多关于这个礼仪[这里](https://*.com/help/someone-answers)。 –

您也可以声明命名空间是这样:

;WITH xmlnamespaces(DEFAULT 'http://schemas.microsoft.com/winfx/2006/xaml/presentation') 
SELECT 
CAST(CAST(Comments AS XML).query('data(/FlowDocument/Paragraph)') AS VARCHAR(7000)) AS activity 
FROM [dbo].Activities where ActivityID=1 

其他选项在这里给出:parsing xml using sql server