?
Solved

How to insert <![CDATA[]]> tags into final XML output

Posted on 2007-03-17
6
Medium Priority
?
594 Views
Last Modified: 2013-11-18
I have a stylesheet that transforms XML from one format to another and works perfectly except that I want my style sheet to place the data within the resulting XML tags within a
<![CDATA[]]> tag so that if the tag is empty my SQLXMLBulkLoad object won't evaluate this tag
value as NULL when it comes to insert the XML into my DB table.

My stylesheet looks like this:

<?xml version='1.0' encoding='ISO-8859-1'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>

<xsl:param name="location" select="'GPOLTD'"></xsl:param>
<xsl:param name="transcode" select="'GPO'"></xsl:param>
<xsl:param name="padzeroes" select ="'00000000000000000000'"></xsl:param>

<xsl:template match="Table">
    <Contributions>
      <xsl:apply-templates select="Row[position()&lt;(last()-1)]"/>
    </Contributions>
</xsl:template>  
 
<xsl:template match="Row">
    <Contribution>
      <xsl:attribute name="num"><xsl:value-of select="position()"/></xsl:attribute>
      <ACHolder>
        <xsl:value-of select="concat($location, substring($padzeroes, 1, 20 - string-length($location) - string-length(Cell[4]/Data)), Cell[4]/Data)"/>
      </ACHolder>
      <EmployeeNo>
        <xsl:value-of select="Cell[1]/Data"/>
      </EmployeeNo>
      <EmployeeName>
        <xsl:value-of select="concat(Cell[2]/Data, ' ', Cell[3]/Data)"/>
      </EmployeeName>
      <EmployeeSurname>
        <xsl:value-of select="Cell[3]/Data"/>
      </EmployeeSurname>      
      <Total>
        <xsl:value-of select="Cell[5]/Data + Cell[6]/Data + Cell[7]/Data"/>       
      </Total>
      <PayDate>
        <xsl:value-of select="Cell[8]/Data"/>             
      </PayDate>
      <TransCode>
        <xsl:value-of select="$transcode"/>
      </TransCode>
      <Comments></Comments>
      <BTA></BTA>
      <Currency></Currency>
      <Field1>
        <xsl:value-of select="Cell[5]/Data"/>      
      </Field1>
      <Field2>
        <xsl:value-of select="Cell[6]/Data"/>      
      </Field2>
      <Field3>
        <xsl:value-of select="Cell[7]/Data"/>      
      </Field3>
      <Field4>0</Field4>       
      <Field5>0</Field5>
    </Contribution>
</xsl:template>  
</xsl:stylesheet>

So what I want to happen is that if any of the values inserted into the atgs above evaluate to empty that the tag ends up looking like
<tag><![CDATA[]]></tag>
and not
<tag></tag>

Many thanks
Sam

0
Comment
Question by:Samm1502
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 18740324
There is no solution to do this in a fine-grained way.
So you can't say: "on this condition make this element a CDATA section"
You can only pick an element and make it CDATA allover the output tree

<xsl:output cdata-section-elements="tag" indent="yes"/>
makes all the tag elements CDATA sections, also when they have content

This should not be a problem. CDATA sections are regarded as any other text nodes during processing.
It strikes me as an odd requirement though.
There is no use in having CDATA if an element is empty... because there is no implicit escaping to be done on an empty string

cheers

Geert

0
 
LVL 10

Expert Comment

by:BobSiemens
ID: 18751709
<tag><![CDATA[]]></tag>
and not
<tag></tag>
==========

Just escape the output characters.
<![CDATA[]]>

IS THE SAME AS

&lt;![CDATA[]]&gt;

(or something very much like that)
0
 
LVL 10

Expert Comment

by:BobSiemens
ID: 18751714
By the way, I agree you should revisit using CDATA.  It's usually a sign of doing things the wrong way.
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 11

Expert Comment

by:Vivek Thangaswamy
ID: 18798847
0
 

Author Comment

by:Samm1502
ID: 18880871
The reason for this is that I am bulk loading my data into a DB table straight from the XML file and the bulkload facility treats empty tags as NULLS when my DB table disallows NULLS on those columns.  I got around this by allowing NULLS on those columns but providing default values so that when the tags are empty the DB inserts the default value and not NULL.

Many thanks and sorry for delayed reply have been unwell.

Sam
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 18880919
welcome
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question