[xsl] XML 2 CSV using XLS with HTML-Tags inside + double quote

Subject: [xsl] XML 2 CSV using XLS with HTML-Tags inside + double quote
From: Eduard Seifert <eddie_s@xxxxxx>
Date: Mon, 12 Nov 2007 01:08:24 +0100
Hello @ all,

first: excuse me for my bad english - i hope you will understand me ;-). It's been a while ago since i wrote a longer text ^^.

Now it's 2 years ago since i was working with XSLT and XML - but the last week i mostly got back into it. My profession is more XHTML + CSS.

To the problem:
- i read the FAQ (all the FAQ's i found)
- used searchengines
to solve my "little" problem.

There are two systems. AuktionMaster and Yatego. Our products are listed in the Database of AuktionMaster. Now i want to get them to another marketplace > Yatego.

There's an option to generate a Eport-Format using XSL. With this XSL i can generate a CSV File. I need this CSV file, because Yatego only imports ;-separated CSV-Files. Ok - i thought - no problem. Well it's frustrating :-(.

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">
<xsl:output method="text" media-type="text/comma-separated-values" version="1.0" indent="yes" encoding="ISO-8859-1" />
<!-- this must be meinly correct i think -->

<!-- fields needed -->
<xsl:template match="/">
		<xsl:apply-templates select="export/item"/>

<xsl:template match="export/item">
<xsl:when test="inventory_active='1'"><!-- i check if product is set flag active -->

... now i catch the fields needed with the fields from the source

	<xsl:value-of select="item_id" /> <!-- for example product number -->

... well, i've got the field names from an other XSL inside of that back-end. So i don't know which version/processor of XSL they use (sorry)

.... ok - i get the fields mainly by <xsl:value-of select="name" />

Now we came to the product description, which is stored with HTML inside the DB. If i use

<xsl:value-of select="description_table/description" disable-output-escaping="yes" />

the HTML Code with instyle CSS <p style="font: 9pt Arial, sans-serif;">This Product is ...</p> and a lot other HTML Tags like <div>, <img>, <ul> ... is breaking inside the CSV-File on the place of the ";" (semicolon). Viewing the file with Excel one part of the HTML Code is inside the field "long_desc" an the rest inside the other fields. So the CSV-File is not correct.

Nearby with method="text" disable-output-escaping="yes" is not working (i read it in the FAQ).

Then i found a lot of Tepmplates how to get the HTML Code inside this one field through escaping quotes.

So i used

<xsl:apply-templates select="description_table/description" mode="escape-CSV" />

... rest of the XSL:

   <xsl:text>&#xD;&#xA;</xsl:text><!-- line break here -->

Here's the Template for escaping the quotes. It worked. But now it doesn't. I don't know why. I never changed it. It's like i found it on the net.

<xsl:template match="node()" mode="escape-CSV" name="escape-CSV">
   <xsl:param name="string" select="." />
      <xsl:when test="contains($string, '&quot;')">
         <xsl:value-of select="substring-before($string, '&quot;')" />
         <xsl:call-template name="escape-CSV">
            <xsl:with-param name="string"
                  select="substring-after($string, '&quot;')" />
         <xsl:value-of select="$string" />

Ok i know i'll get it back to work - but the main problem is is: After escaping the html-code it shows inside the CSV &lt;p&gt; ... . Because the CSV is going to be importet in Yatego i need to replace the field (string) and get the < and > back. So i tried and tried. And i didn't get it. I know the problem isn't much big - but i need the "Aaaaaah"-Effect to go on :-).

Here is one of the ideas i tried:

<xsl:template match="node()" mode="escape_special_characters" name="escape_special_characters">
<xsl:param name="input_text" select="." />
<!-- http://www.w3schools.com/xsl/el_param.asp -->
<xsl:variable name="step_1">
<xsl:call-template name="replace_quot">
<xsl:with-param name="input_text" select="." />
<xsl:value-of select="$step_1" />

<xsl:template name="replace_lt">
<xsl:param name="input_text" />
<xsl:param name="special_char" />
<xsl:when test="contains($input_text, $special_char)">
<xsl:value-of select="substring-before($input_text, $special_char)" />
<xsl:value-of select="$special_char" />
<xsl:call-template name="replace_lt">
<xsl:with-param name="input_text" select="substring-after($input_text, $special_char)" />
<xsl:with-param name="special_char" select="$special_char" />
<xsl:value-of select="$input_text" />


I think my main problem is not the recursive part it's the basic part of calling the template:

<xsl:apply-templates select="description_table/description" mode="escape-CSV" />

I don't know how to get all the templates to work or how correctly to call them for:

- 1. double quote the HTML-Code to get something linke "&lgt;p&gt; ... "
- 2. replace &lt; with < and &gt; with > to get correct HTML Code inside the CSV
- 3. to import the CSV into Yatego to get the product description displayed correctly in the browser.

Thank you all very very much for all kind of help.


Current Thread