Re: [xsl] Replace new lines by <br> and double quote with special char: Problem retaining HTML tags

Subject: Re: [xsl] Replace new lines by <br> and double quote with special char: Problem retaining HTML tags
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Fri, 08 Sep 2006 15:56:37 +0200
Hi Ambika,

This is indeed the good way of posting the question, all is clear from here. With XSLT 1.0, I think you will have some challenges, especially if you want to deal with these special characters that you mention in the test. Here's a template that gives you the output as you specified it. You probably, most likely, want to alter it to you needs. I expect that you want the transformXMLString template to be recursive (now it only checks if there is only one element that is escaped with &lt; and &gt;).

Two important lessons for when you want to export as CSV with intermixed HTML (with XML-wise illegal tags as <br>):
1. Use output method = "text", otherwise you will never get the textual output, but only HTML/XML in which you have no control over the whitespace etc.
2. Do not use <xsl:copy>. It will copy all nodes as they are and as nodes, and you want text, not nodes as an output. Use template matching instead.


When you respond to this message, keep in mind that the experts on this list can only answer if you provide enough information. At a bare minimum:
- input, output, stylesheet
- what you tried, why it failed,
- whose suggestions you've tried and what you expected and what the actual results are.


BTW: the template as I suggest it, is not the only available solution. There are many ways that lead to Rome.

Cheers,
Abel Braaksma
http://abelleba.metacarpus.com

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";> <xsl:output method="text" indent="no"/>
<xsl:template match="/">
<xsl:text>Elem Id, New Details&#x0a;</xsl:text>
<xsl:apply-templates select="elem" />
</xsl:template>


<xsl:template match="elem">
<xsl:text>&quot;</xsl:text>
<xsl:apply-templates select="//p/text()"/>
<xsl:text>&quot;</xsl:text>
</xsl:template>
<xsl:template match="p/text()">
<![CDATA[<p>]]>
<xsl:call-template name="transformXMLString">
<xsl:with-param name="StringToTransform" select="." />
</xsl:call-template><![CDATA[</p><br>]]>
</xsl:template>
<xsl:template name="transformXMLString">
<xsl:param name="StringToTransform" />
<xsl:if test="contains(., '&lt;')" >
<xsl:value-of select="substring-before(., '&lt;')" />
<![CDATA[&lt;]]>
<xsl:value-of select="substring-after(substring-before(., '&gt;'), '&lt;')" />
<![CDATA[&gt;]]>
<xsl:value-of select="substring-after(., '&gt;')" />
</xsl:if>
</xsl:template>
</xsl:stylesheet>




Ambika.Das@xxxxxxxxxxxxxxxxxx wrote:
Hi Abel,

Please find the code below.

XML

<?xml version="1.0" encoding="UTF-8" ?>
<elem id="1234" date="14 Aug 2006" time="13:36">

<title>Sample Title</title>
<text>
<p> This is some data </p>
<p> </p>
<p> This is some special characters &lt;AHLN.AS&gt; </p>
<p> A group of students have gone to picnic. </p>
<p> </p>
<PRE> ** This is another special tag
</PRE></text>
</elem>


XSL

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";> <xsl:output method="html" indent="no"/>
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="p//text()">
<xsl:call-template name="transformXMLString">
<xsl:with-param name="StringToTransform" select="."/>
</xsl:call-template>
</xsl:template>


  <xsl:template  name="transformXMLString">
   <xsl:param name="StringToTransform" select="."/>
   <!--
   logic for replacing line break and double quote
   -->
  </xsl:template>
</xsl:stylesheet>

Current output

<elem id="1234" date="14 Aug 2006" time="13:36">

<title>Sample Title</title>
<text>
<p> This is some data </p>
<p> </p>
<p> This is some special characters &lt;AHLN.AS&gt; </p>
<p> A group of students have gone to picnic. </p>
<p> </p>
<PRE> ** This is another special tag
</PRE></text>
</elem>


Desired Output

Elem Id, News Details
1234, "<p>    This is some data </p><br><p></p><br><p>    This is some
special characters &lt;AHLN.AS&gt; </p><br><p>    A group of students
have gone to picnic. </p><br><p>     </p><br><PRE>    ** This is another
special tag<br></PRE></text><br></elem>"

The header should be in one line and the data in one line(CSV like
output).


The logic for replacing line break and double quote is simple recursive
function and I don't think I need to paste it here. It will
unnecessarily make the mail big.


Thanks & Regards, Ambika Prasad Das

-----Original Message-----
From: Abel Braaksma [mailto:abel.online@xxxxxxxxx] Sent: Friday, September 08, 2006 5:29 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Replace new lines by <br> and double quote with
special char: Problem retaining HTML tags


Ambika.Das@xxxxxxxxxxxxxxxxxx wrote:
My apologies. There was a typo in the XML. Once again I am giving the
input and output
Please be attentive to what you submit, and read through the replies you

get before answering. David has given you a possible solution. Just resubmitting the same question with corrected inp/outp will not really help, as it is only part of the story.

In some of the postings you asked "is it possible to do so in xslt". Answer: yes
In most of your questions you ask how to embed html (or xhtml?) inside CSV-like output. This is possible also. My guess is that David's reply is just about that (but I didn't try it myself. He did, but more importantly: *you* should try it).


Since you don't supply the requested XSLT, the part that you tried yourself and what errors you got or where it went wrong, and since we have no clue whatsoever of whose answers where helpful and whose weren't

and why, it is impossible to guide you further. Unless of course you decide to respond to the questions you have got, you supply the XSLT that you have so far and you reply to the suggested solutions and what your findings where.

Cheers,

Abel Braaksma
http://abelleba.metacarpus.com
-----Original Message-----
From: David Carlisle [mailto:davidc@xxxxxxxxx] Sent: Thursday, September 07, 2006 6:46 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Required help in XSL transformation



Any clues how to implement the same?
did you try the template I posted earlier? Why start a new thread with
an identical question?

Current Thread