Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to encapsulate xml using CDATA in xslt

Status
Not open for further replies.

seattledev

Programmer
Jul 18, 2007
2
US
Hi there,

I am trying to use an xslt to wrap some XML in a CDATA element. Such as transforming:

<A>
<B val1="1"></B>
<C>some text</C>
</A>

to:

<A><![CDATA[
<B val1="1"></B>
<C>some text</C>
]]
</A>

I've done a fair amount of googling and can't seem to find out how to do this..

Thanks,
Craig
 
I would offer a solution using xslt extension functionality, in particular, ms script extension. Otherwise, it is a very tedious recursive work with string manipulation within xslt 1.0.
[tt]
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl=" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="urn:tsuji-test:test">

<msxsl:script language="vbscript" implements-prefix="user">
<![CDATA[
function serialize(onodelist)
dim s
s=vbcrlf & vbtab
for i=0 to onodelist.length-1
s=s & onodelist(i).xml & vbcrlf
if i<>onodelist.length-1 then
s=s & vbtab
end if
next
serialize=s
end function
]]>
</msxsl:script>

<xsl:eek:utput method="xml" encoding="utf-8" version="1.0" cdata-section-elements="A" indent="yes" />
<xsl:template match="/">
<xsl:apply-templates select="*" />
</xsl:template>
<xsl:template match="A">
<xsl:variable name="s_serialized" select="user:serialize(*)" />
<xsl:copy>
<xsl:value-of select="$s_serialized" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
[/tt]
As to the choice of what kind of extension based on the platform and environment, if you're not using msxml2 core service, you have to do some adaptation.

People may or may not appreciate this approach. It is up to you.
 
Craig said:
I've done a fair amount of googling and can't seem to find out how to do this..

Craig, I know this may be a situation where you have no control over this requirement, but this requirement almost always indicates a design failure somewhere.

So, do you have any control over this?

Tom Morrison
 
>Craig, I know this may be a situation where you have no >control over this requirement, but this requirement almost >always indicates a design failure somewhere.
>So, do you have any control over this?

What I am doing is taking XML data and loading it in bulk into SQL Server 2005 tables with the use of datasets with XSDs providing the type information and table structure.

But the way datasets work is that each node level creates a new table and in some cases a new table isn't warranted because it's fairly unlikely the data will be used and it's also rather noncompliant xml. Plus the tables will include all nodes at this level so you lose the ability to know in what subnode they were at.

So my thought is to transform the data by wrapping the unwanted xml in CDATA and in the xsd file treat it like a text field and then just stuff it into an xml column in the table (after stripping off the CDATA) so I can use xquery to query it later should the unlikely need arise.

This might seem a little overly complicated but the alternative of using XPath to parse these files was impractical due to the sheer number of different kinds of log files I need to handle.
 
Hi seattledev,

The solution to your problem is the cryptic "disable-output-escaping" attribute, which will transform &gt; to ">" in the output.

An XSLT matching your original question would therefore be :
[tt]<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:eek:utput method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="* | text() | @*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="A">
<xsl:copy>
<xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
<xsl:apply-templates/>
<xsl:text disable-output-escaping="yes">&#x5D;&#x5D;&gt;</xsl:text>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>[/tt]

Paul
There are three kind of mathematicians : those who can count, and those who cannot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top