Facebook Twitter Sign in | Join
SQL Backup Pro
Getting started with SSIS - Part 1: Introduction to SSIS
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.

Jacob's Blog

My technology blog on SQL Server, TSQL, XML, FILESTREAM and other areas of SQL Server.
Connect your existing Source Control system to SSMS in 5 minutes
Sponsored [Advertise Here]
Learn XSD and XML Schema Collections
beyondrelational.com
This book helps you to learn XML Schema Collections from basic to advanced levels through simple examples and easy to follow walk through labs.

Syndicate your blog!
beyondrelational.com
Syndicate your blog with us to get wider reach into the technology community. Click here to get started.

SSRS Tutorial - Getting Started with SQL Server Reporting Services
beyondrelational.com
This tutorial will help you to get started with SQL Server Reporting Services (SSRS)
Loading

How to generate CDATA and NAMESPACE Declarations using FOR XML EXPLICIT

I was asked this question in one of the SQL Server forums recently. The OP wanted to generate an XML document with CDATA sections and NAMESPACE declarations.

SQL Server 2005 introduced WITH XMLNAMESPACES() which is very handy to generate namespace declarations in the output XML document generated by FOR XML. FOR XML EXPLICIT can be used to generate CDATA sections in the XML output. However, what is interesting is that you cannot use WITH XMLNAMESPACES and FOR XML EXPLICIT together.

Here is a FOR XML EXPLICIT query that generate CDATA sections as well as XML NAMESPACE declarations in the output XML document.

SELECT
	1 AS Tag,
	NULL AS Parent,
	'Jacob' AS 'Person!1!Name!element',
	'Hello world' AS 'Person!1!Greeting!CDATA',
	'http://beyondrelational.com/xml.aspx' 
		AS 'Person!1!xmlns:x'
FOR XML EXPLICIT

Here is the output

<Person xmlns:x="http://beyondrelational.com/xml.aspx">
  <Name>Jacob</Name>
  <Greeting><![CDATA[Hello world]]></Greeting>
</Person>

Jacob Sebastian
1 · 100% · 16868
1
Liked
0
Comments



Submit

     

Subscribe to Notifications

Your Comment


Sign Up or Login to post a comment.

Copyright © Beyondrelational.com