[xsl] Converting markup to non-markup

Subject: [xsl] Converting markup to non-markup
From: Lars Eskildsen <laes@xxxxxxxxx>
Date: Thu, 12 Sep 2002 15:02:58 +0200
Hello!

I want to be able to convert an XML snippet like:

<CONV_TAG>
    <TAG1 ATTR1="VAL1">Statement: 4 &lt; 5</TAG1>
<CONV_TAG>

to this XML code:

<CONVERTED_TAG>
   &lt;TAG1 ATTR1=&quot;VAL1&quot;&gt;Statement: 4 &amp;lt; 5&lt;/TAG1&gt;
</CONVERTED_TAG>

I have no problem with the "entifying" of the original markup - i use the XSL templates below)

----------------------------
<xsl:template match="node()" mode="xml-to-text">&lt;<xsl:value-of select="name()"/><xsl:apply-templates select="@*" mode="xml-to-text"/>&gt;<xsl:apply-templates select="node()"
mode="xml-to-text"/>&lt;/<xsl:value-of select="name()"/>&gt;</xsl:template>

<xsl:template match="@*" mode="xml-to-text"><xsl:text> </xsl:text><xsl:value-of select="name()"/>=<xsl:text disable-output-escaping="yes"><![CDATA[&quot;]]></xsl:text><xsl:value-of
select="."/><xsl:text disable-output-escaping="yes"><![CDATA[&quot;]]> </xsl:text></xsl:template>

<xsl:template match="text()" mode="xml-to-text"><xsl:value-of select="."/></xsl:template>
---------------------------

The problem is i want to convert standard entities inside non-markup content. 

&lt;	to 	&lt;lt;
&gt;	to 	&gt;gt;
&amp;	to	&amp;amp; 

...and so on. 

I therefore changed the third rule (in the above) which is the one that deals with non-markup content,
 to the following two templates:
--------------------------------------
<xsl:template match="text()" mode="xml-to-text">
  <xsl:variable name="NodeText"><xsl:value-of select="normalize-space(.)"/></xsl:variable>
  <xsl:call-template name="Entify">
    <xsl:with-param name="TheText" select="$NodeText">
    </xsl:with-param>
  </xsl:call-template>
</xsl:template>

<xsl:template name="Entify">
<xsl:variable name="TheText"><xsl:value-of select="'AAA'"/></xsl:variable>
   <xsl:choose>
     <xsl:when test="substring-before($TheText, '&lt;')">
        <xsl:value-of select="concat(substring-before($TheText, '&lt;'), '&lt;lt')"/>
        <xsl<xsl:value-of select="substring-after($TheText, '&lt;')"/>
          </xsl:with-param>ame="TheText">
        </xsl:call-template>
     </xsl:when>
     <xsl:when test="substring-before($TheText, '&gt;')">
        <xsl:value-of select="concat(substring-before($TheText, '&lt;'), '&gt;gt;')"/>
        <xsl:call-template name="Entify">
          <x<xsl:value-of select="substring-after($TheText, '&gt;')"/>
          </xsl:with-param>
        </xsl:call-template>
     </xsl:when>
     <xsl:when test="substring-before($TheText, '&amp;')">
        <xsl:value-of select="concat(substring-before($TheText, '&amp;'), '&amp;amp;')"/>
        <xsl:call-template name="Entify">
          <x<xsl:value-of select="substring-after($TheText, '&amp;')"/>
          </xsl:with-param>
        </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
        <xsl:value-of select="$TheText"/>
     </xsl:otherwise>
   </xsl:choose>
</xsl:template>
-----------------------------------------------------

The problem is that the macro template variable/parameter TheText always seems to be undefined in
connection with the template macro call and therefore receive the value 'AAA' inside the macro template.
Then all my non-markup content is converted to 'AAA' !

What is wrong with this approach - can anybody please help me!!

/Lars

** Stibo Graphic          | Søren Nymarks Vej 21 | DK-8270 Højbjerg 
** mailto:laes@xxxxxxxxx  | http://www.stibographic.com 
** Phone:  +45 8939 8939  | Fax:    +45 8939 8940
** Direct: +45 8939 7421


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread