Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2041
  • Last Modified:

XSLT removes CDATA Tags

Hi

I have an output file of XML which looks like

<CR><CR_NAME><![CDATA["INT-Afghanistan"]]></CR_NAME><CR_SRC><![CDATA[" 98289383"]]></CR_SRC><CR_SRCLEN><![CDATA[" "]]></CR_SRCLEN><CR_SRCPREFIX><![CDATA[" "]]></CR_SRCPREFIX><CR_DEST><![CDATA["93"]]></CR_DEST><CR_DESTLEN><![CDATA[" "]]></CR_DESTLEN><CR_PREFIX><![CDATA["93"]]></CR_PREFIX><CR_FLAGS><![CDATA["32"]]></CR_FLAGS><CR_CPNAME><![CDATA[" "]]></CR_CPNAME><CR_TRNAME><![CDATA[" "]]></CR_TRNAME><MTIME><![CDATA["2222222222"]]></MTIME></CR>

Using the XSLT pasted at the bottom script it formats the file to look like
      <CR>
            <CR_NAME> "INT-Afghanistan" </CR_NAME>
            <CR_SRC> "98289383" </CR_SRC>
            <CR_SRCLEN> "0" </CR_SRCLEN>
            <CR_SRCPREFIX> "98289383" </CR_SRCPREFIX>
            <CR_DEST> "" </CR_DEST>
            <CR_DESTLEN> "0" </CR_DESTLEN>
            <CR_PREFIX> "" </CR_PREFIX>
            <CR_FLAGS> "16" </CR_FLAGS>
            <CR_CPNAME> "" </CR_CPNAME>
            <CR_TRNAME> "" </CR_TRNAME>
      </CR>

What I need is for it to output like

<CR>
            <CR_NAME><![CDATA["INT-Afghanistan"]]></CR_NAME>
            <CR_SRC><![CDATA[" "]]></CR_SRC>
            <CR_SRCLEN><![CDATA[" "]]></CR_SRCLEN>
            <CR_SRCPREFIX><![CDATA[" "]]></CR_SRCPREFIX>
            <CR_DEST><![CDATA["93"]]></CR_DEST>
            <CR_DESTLEN><![CDATA[" "]]></CR_DESTLEN>
            <CR_PREFIX><![CDATA["93"]]></CR_PREFIX>
            <CR_FLAGS><![CDATA["32"]]></CR_FLAGS>
            <CR_CPNAME><![CDATA[" "]]></CR_CPNAME>
            <CR_TRNAME><![CDATA[" "]]></CR_TRNAME>
            <MTIME><![CDATA["2222222222"]]></MTIME>
      </CR>

What needs to change in the XSLT file to do this????
Cheers
Matt


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
     version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:sql="urn:schemas-microsoft-com:xml-sql"
     exclude-result-prefixes="sql"
     >
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

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

<xsl:template match="@*">
     <xsl:copy>
     </xsl:copy>
</xsl:template>

<xsl:template match="*">
     <xsl:element name="{local-name()}">
          <xsl:apply-templates select="@*"/>
          <xsl:apply-templates select="node()"/>
     </xsl:element>
</xsl:template>

</xsl:stylesheet>
0
pclarkin
Asked:
pclarkin
  • 2
1 Solution
 
metalmickeyCommented:
Creating CDATA sections with XSLT

http://www.azureus.com/xml/art_creating_cdata_sections_in_xslt.htm


See examples 5 and 6


Hope this helps

MM
0
 
pclarkinAuthor Commented:
OK that makes sense. Now I need to work out how to get the <cdata> tags into my <cr>
so it needs to look like
<CR>
          <CR_NAME>
                            <cdata>
                                        <![CDATA["INT-Afghanistan"]]>
                             </cdata>
          </CR_NAME>
etc.

At the moment it's generated from SQL Server using the for xml explicit command like

SELECT 1                  as Tag,
       NULL                 as Parent,
      '<![CDATA[' + CR_NAME + ']]>' as [CR!1!CR_NAME!XML],
etc
FROM CR
FOR XML EXPLICIT

How do I nest this cdata tag in?
0
 
pclarkinAuthor Commented:
Don't worry - I see you can put multiple entrys in the cdata-section-elements declaration.

Cheers.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now