p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   replace ' and " with \' and \" (http://p2p.wrox.com/showthread.php?t=42927)

Brian Campbell May 23rd, 2006 04:22 PM

replace ' and " with \' and \"
 
I'm new to XSLT...I have values in my XML that i'm trying to pass via javascript...however, to do so i need to escape double and single quotes. i found the following code, but i have no idea how to implement it.

Code:

<xsl:template name=“common_js_escape”>
        <!– required –>
        <xsl:param name=“text”/>
        <xsl:variable name=“tmp”>

                <xsl:call-template name=“replace-substring”>
                        <xsl:with-param name=“from” select=“‘&quot;’”/>
                        <xsl:with-param name=“to”>\"</xsl:with-param>
                        <xsl:with-param name=“value”>
                                <xsl:call-template name=“replace-substring”>
                                        <xsl:with-param name=“from”>&apos;</xsl:with-param>
                                        <xsl:with-param name=“to”>\’</xsl:with-param>
                                        <xsl:with-param name=“value” select=“$text” />
                                </xsl:call-template>
                        </xsl:with-param>
                </xsl:call-template>

        </xsl:variable>
        <xsl:value-of select=“$tmp” />
</xsl:template>

and that template seems to rely on this one...

Code:

<xsl:template name=“replace-substring”>
      <xsl:param name=“value” />
      <xsl:param name=“from” />
      <xsl:param name=“to” />
      <xsl:choose>
         <xsl:when test=“contains($value,$from)”>
            <xsl:value-of select=“substring-before($value,$from)” />
            <xsl:value-of select=“$to” />
            <xsl:call-template name=“replace-substring”>
               <xsl:with-param name=“value” select=“substring-after($value,$from)” />
               <xsl:with-param name=“from” select=“$from” />
               <xsl:with-param name=“to” select=“$to” />
            </xsl:call-template>
         </xsl:when>
         <xsl:otherwise>
            <xsl:value-of select=“$value” />
         </xsl:otherwise>
      </xsl:choose>
</xsl:template>

i need to apply it to <xsl:value-of select="description" /> and <xsl:value-of select="title" /> ...can anyone dumb this down for me? :)

mhkay May 24th, 2006 04:09 AM

You just need to call your template, for example

<xsl:variable name="$result">
  <xsl:call-template name="common_js_escape">
    <xsl:with-param name="text" select="description"/>
  </xsl:call-template>
</xsl:variable>

<xsl:value-of select="$result"/>

will output the value "description" after doing the relevant substitutions.

(In fact, you only need the variable if you are doing something with the result other than xsl:value-of. If you do the call-template without a variable, the result will simply be written to the result tree.)

It's a lot easier in XSLT 2.0 which has a replace() function.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

Brian Campbell May 24th, 2006 10:09 AM

Ok, this is making more sense...now, when you say "written back to the result tree", does this mean that after calling "common_js_escape" and applying it to "desicription" i can just call "description" (<xsl:value-of select="description"/>) again later in the code and it will be escaped? ...or do i have to enclose it in the call to the function? Thanks for your help!

mhkay May 24th, 2006 10:39 AM

No, the value of description is not modified. You have to capture the result of applying the template and use that. That's why I showed you how to capture it in a variable.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

Brian Campbell May 24th, 2006 10:49 AM

Thanks for your help michael! Much appreciated...i may have to pick up your books...definitely need the reference material :)



All times are GMT -4. The time now is 01:33 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.