Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 23rd, 2006, 04:22 PM
Authorized User
 
Join Date: Oct 2004
Location: Denver, CO, USA.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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? :)
  #2 (permalink)  
Old May 24th, 2006, 04:09 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
  #3 (permalink)  
Old May 24th, 2006, 10:09 AM
Authorized User
 
Join Date: Oct 2004
Location: Denver, CO, USA.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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!
  #4 (permalink)  
Old May 24th, 2006, 10:39 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
  #5 (permalink)  
Old May 24th, 2006, 10:49 AM
Authorized User
 
Join Date: Oct 2004
Location: Denver, CO, USA.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace In c# jayanth_nadig SQL Language 2 July 3rd, 2006 03:25 AM
replace "." with "/" thelos Excel VBA 1 September 14th, 2005 10:47 AM
Replace crmpicco VB How-To 15 May 20th, 2005 01:34 PM
replace() Missy_K Javascript 1 January 16th, 2004 11:17 AM





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