Re: encoding $ character for wml output via xsl:value-of

Subject: Re: encoding $ character for wml output via xsl:value-of
From: Stephen Zisk <szisk@xxxxxxxxxxxxxxx>
Date: Fri, 01 Sep 2000 15:23:36 -0400

WML syntax uses the $ (dollar sign) for de-referencing a variable.  To
display the character, $$ must be specified, otherwise most WML browsers
return an error.  If an xml file containing a $ in a node is transformed via
XSL (using the xsl:value-of statement) to produce WML, the $ is output as a
$.  Other characters such as &, <, " are output in the encoded form.  Does a
special encoding scheme exist to force the output of a $ in encoded format?
Does some other mechanism exist to allow character transformations to be
customized within an xsl:value-of operation?  Pre-processing the xml file is
an issue as the vast majority of xml inputs do not contain a $.  Post
processing the transformed output is also an issue as the output does
contain WML variable references using the $.

This has been answered before, and perhaps should be in the FAQ.


One simple way, using the XPath string operators, is to call a template that finds the first '$' in a string, replaces it with '$$', and recurses on itself with the remainder of the string:

First, for each string that might contain '$', call the "dollar" template within your XSL to fix it:

<block>
  <xsl:call-template name="dollar">
    <xsl:with-param name="text" select="ELEMENT_TO_FIX" />
  </xsl:call-template>
</block>

Substitute whatever you want checked for the select above. This call to dollar must appear wherever you want the text to be searched.

Next, add a "dollar" template (as called above) into the XSL file, which does a recursive search for '$', replacing it with '$$':

<xsl:template match="text()" name="dollar">
  <xsl:param name="text" select="."/>
  <xsl:choose>
    <xsl:when test="contains($text,'$')">
      <xsl:value-of select="substring-before($text,'$')"/>
      <xsl:text>$$</xsl:text>
      <xsl:call-template name="dollar">
        <xsl:with-param name="text" select="substring-after($text,'$')"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$text"/>
   </xsl:otherwise>
   </xsl:choose>
</xsl:template>

Regards,
Stephen Zisk


---------- Stephen Zisk MediaBridge Technologies email: szisk@xxxxxxxxxxxxxxx 100 Nagog Park tel: 978-795-7040 Acton, MA 01720 USA fax: 978-795-7100 http://www.mediabridge.net


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



Current Thread