简体   繁体   English

使用命名空间SQL Server进行XML解析

[英]XML parsing with namespace SQL Server

We are cleaning up data in our database and a column has XML details inside of it which we want to be able to convert into plain text. 我们正在清理数据库中的数据,并且一列中包含XML详细信息,我们希望能够将其转换为纯文本。

Below is the sample XML in the table column. 下面是表格列中的示例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>

I am using this query, but it is not rendering the desired output due to the presence of Namespace (if I remove the namespace from the XML, I am able to render the output successfully). 我正在使用此查询,但是由于存在命名空间,它无法呈现所需的输出(如果我从XML中删除了命名空间,则能够成功呈现输出)。

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

Kindly help in this matter. 请对此事提供帮助。

Thanks 谢谢

You can also declare your namespace like this: 您还可以这样声明您的名称空间:

;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

Other options are given here: parsing xml using sql server 这里提供了其他选项: 使用sql server解析xml

You need to use namespace declaration in your Query as per: https://msdn.microsoft.com/en-us/library/ms191474.aspx 您需要按照以下说明在查询中使用名称空间声明: https : //msdn.microsoft.com/zh-cn/library/ms191474.aspx

so your query portion would look something like: 因此您的查询部分将类似于:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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