1

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.

Below is the sample XML in the table column.

 <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).

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

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

2

You need to use namespace declaration in your Query as per: https://msdn.microsoft.com/en-us/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)
') 

1 Comment

You are very welcome. Please consider accepting the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.