You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by "Daniel F. Jones" <da...@cs.utexas.edu> on 2000/02/21 09:10:47 UTC

Newbie Question: XSL Translation

I need to replace all $ with $$ so that prices will display correctly in
WML.

For example, the XSL translation of

<product>
  <price>$100.00</price>
</product>  

produces an error in WML when using <xsl:apply-templates/> because the WML
digest compiler can't find the variable "100.00"

Thanks, Dan




Re: Newbie Question: XSL Translation

Posted by Phil Lanch <ph...@aldigital.co.uk>.
Mike Engelhart wrote:
> 
> Daniel F. Jones wrote:
> 
> > I need to replace all $ with $$ so that prices will display correctly in
> > WML.
> >
> > For example, the XSL translation of
> >
> > <product>
> > <price>$100.00</price>
> > </product>
> >
> > produces an error in WML when using <xsl:apply-templates/> because the WML
> > digest compiler can't find the variable "100.00"
> 
> If you can, you shouldn't put the currency in with the price so that you can
> localize it later.
> 
> I would do something like:
> <product>
>     <price currency="USD">100.00</price>
> </product>
> 
> and then use the stylesheet to add the $ or whatever currency is added
> later.

-that's definitely a better way.  but if you really can't avoid it, you
could do something (very inefficient) like-

<xsl:template match="text()" name="text">
  <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="text">
        <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>

this looks for prices everywhere in the source doc - which is probably
far too general.  if you know they will only occur in certain places,
you could (to lose some inefficiency) only run this template when there
may be prices present by adding a mode="price" attribute to the
template, and doing <xsl:apply-templates select="text()" mode="price"/>
where there may be prices.

-- 

cheers

phil

"I have remarked very clearly that I am often of one opinion
when I am lying down and of another when I am standing up ..."

Re: Newbie Question: XSL Translation

Posted by Mike Engelhart <me...@earthtrip.com>.
Daniel F. Jones wrote:

> I need to replace all $ with $$ so that prices will display correctly in
> WML.
> 
> For example, the XSL translation of
> 
> <product>
> <price>$100.00</price>
> </product>  
> 
> produces an error in WML when using <xsl:apply-templates/> because the WML
> digest compiler can't find the variable "100.00"
> 
> Thanks, Dan

If you can, you shouldn't put the currency in with the price so that you can
localize it later. 

I would do something like:
<product>
    <price currency="USD">100.00</price>
</product>

and then use the stylesheet to add the $ or whatever currency is added
later.

Mike