Solved

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

Posted on 2007-03-17
6
588 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 …
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

724 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