Solved
XSL - using CDATA or other to escape message otherwise seen as markup
Posted on 2013-06-25
I'm trying to use CDATA to escape a block of text that would otherwise be interpreted as markup.
I have the following .xsl
<xsl:template match="Response">
<Request>
MI
<xsl:call-template name="strip-between-tags">
<xsl:with-param name="str" select="."/>
</xsl:call-template>
</Request>
</xsl:template>
<xsl:template name="strip-between-tags">
<xsl:param name="str"/>
<xsl:if test="contains($str, '<')">
<xsl:text disable-output-escaping="yes"><</xsl:text>
<xsl:value-of select="substring-before(substring-after($str, '<'), '>')"></xsl:value-of>
<xsl:text disable-output-escaping="yes">></xsl:text>
<xsl:call-template name="strip-between-tags">
<xsl:with-param name="str" select="substring-after($str, '>')"/>
</xsl:call-template>
</xsl:if>
<xsl:if test="not (contains($str, '<'))">
<xsl:text disable-output-escaping="yes"> <X> </xsl:text>
</xsl:if>
</xsl:template>
This is my input data that I'm feeding into the xsl:
<CommandRS Version="2003.XML1.0.1">
<Response>MI<X> X IF SELECT NAME <CAD>< ></Response>
</CommandRS>
Produces output
<CommandRQ Version="2003.XML.0.1">
<Request>MI<X><CAD>< > <X> </Request>
</CommandRQ>
However, the output is erroring out because of the message between the Request tags.
So I tried changing my xsl to include CDATA:
<xsl:template match="Response">
<Request>
<![CDATA[
MI
<xsl:call-template name="strip-between-tags">
<xsl:with-param name="str" select="."/>
</xsl:call-template>
]]>
</Request>
</xsl:template>
but this would comment out my template call. Please advise on how I can escape the block of text using CDATA or other method.
I also tried
<xsl:template match="Response">
<Request>
<xsl:text><![CDATA[ ]]></xsl:text>
MI
<xsl:call-template name="strip-between-tags">
<xsl:with-param name="str" select="."/>
</xsl:call-template>
</Request>
</xsl:template>
but the output never included the "![CDATA[". Not sure how to implement this into the xsl