RE: [xsl] Problem retaining HTML tags during XSL transformation

Subject: RE: [xsl] Problem retaining HTML tags during XSL transformation
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 10 Jan 2007 16:35:23 -0000
It's not clear to me what you're trying to do, so it's hard to suggest how
you should be doing it. But by passing an element containing mixed content
to a template such as transformXMLString that treats it as a string, you are
implicitly extracting the string value of the element - that is, you are
flattening the tree structure into a string, which destroys all the elements
and means there are no tags in the serialized result.

If you want to process the content of the text nodes, you have to do it one
node at a time.

Michael Kay
http://www.saxonica.com/

> -----Original Message-----
> From: Ambika.Das@xxxxxxxxxxxxxxxxxx
> [mailto:Ambika.Das@xxxxxxxxxxxxxxxxxx]
> Sent: 10 January 2007 16:16
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Problem retaining HTML tags during XSL transformation
>
> Hi All,
>
> I am facing some problem in retaining the HTML tags during
> XSL transformation.
> Given below are the details.
>
> Input XML:
>
> <?xml version="1.0" encoding="UTF-8" ?>
> <elem id="11" date="10 Jan 2007" time="16:55"> Non Title
> <title>Title Here</title> <text> <PRE> (Start of pre tag) <p>
> Within P </p> After P tag (End of pre tag) </PRE></text> </elem>
>
> XSL Code:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <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="elem">
> 		<xsl:text>Elem Id, News Details&#10;</xsl:text>
> 		<xsl:value-of select="@id"/>
> 		<xsl:text>,"</xsl:text>
> 		<xsl:call-template name="transformXMLString">
> 		    <xsl:with-param name="StringToTransform"
> select="$doubleQtReplaced"/>
> 		</xsl:call-template>
> 		<xsl:text>"</xsl:text>
> 	</xsl:template>
>
> 	<xsl:variable name="doubleQtReplaced">
>         <xsl:call-template name="replaceDblQt">
>             <xsl:with-param name="StringToTransform" select="/"/>
>         </xsl:call-template>
>     </xsl:variable>
>
> 	<xsl:template match="error">
>
> <xsl:text>SYSERROR_TYPE,SYSERROR_CODE,SYSERROR_DESC&#10;X,730,
> "</xsl:text>
> 		    <xsl:value-of select="error_text"/>
> 		<xsl:text>"</xsl:text>
> 	</xsl:template>
>
> 	<xsl:template  name="transformXMLString">
> 	    <xsl:param name="StringToTransform" select="."/>
> 	    <xsl:choose>
>             <xsl:when test="contains($StringToTransform,'&#10;')">
>                 <xsl:value-of
> select="substring-before($StringToTransform,'&#10;')"
> /><br/><xsl:call-template name="transformXMLString">
>                         <xsl:with-param name="StringToTransform">
>                             <xsl:value-of
> select="substring-after($StringToTransform,'&#10;')" />
>                         </xsl:with-param>
>                     </xsl:call-template>
>             </xsl:when>
> 	        <xsl:otherwise>
> 	            <xsl:value-of select="$StringToTransform"/>
> 	        </xsl:otherwise>
> 	    </xsl:choose>
> 	</xsl:template>
>
> 	<xsl:template name="replaceDblQt">
> 	    <xsl:param name="StringToTransform" select ="."/>
> 	    <xsl:choose>
> 	        <xsl:when test="contains($StringToTransform,'&#x22;')">
>                 <xsl:value-of
> select="substring-before($StringToTransform,'&#x22;')"/>""<xsl
> :call-template name="replaceDblQt">
>                         <xsl:with-param name="StringToTransform">
>                             <xsl:value-of
> select="substring-after($StringToTransform,'&#x22;')"/>
>                         </xsl:with-param>
>                     </xsl:call-template>
>             </xsl:when>
>             <xsl:otherwise>
> 	            <xsl:value-of select="$StringToTransform"/>
> 	        </xsl:otherwise>
> 	    </xsl:choose>
> 	</xsl:template>
>
> </xsl:stylesheet>
>
> Current Output:
>
> Elem Id, News Details
>
> 11,"<br>Non Title<br>Title Here<br><br> (Start of pre tag)
> <br><br>Within P<br><br>After P tag<br>(End of pre tag) <br>"
>
> Desired output:
>
> Elem Id, News Details
>
> 11,"<br>Non Title<br><title>Title Here</title><br><br>
> <PRE>(Start of pre tag) <br><p>Within P</p><br>After P
> tag<br>(End of pre tag) </PRE> <br>"
>
> Any clues?
>
> Thanks and Regards,
>  
> A P Das

Current Thread