Solved

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

Posted on 2007-03-17
6
581 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
6 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Consuming WCF service from Java with callback contract 5 268
Problem to picture file 3 73
Fixed div within Bootstrap carousel item 11 58
Cordova Camera plugin fails 2 115
When you work with shopping cart / ecommerce relates web sites, you need to pass the certain form post details to the payment gateway process page with required details for the products items you give to order. Also you may need to track the ordered…
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
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.

815 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now