Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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? :)
Reply With Quote
  #2 (permalink)  
Old May 24th, 2006, 04:09 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
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
Reply With Quote
  #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!
Reply With Quote
  #4 (permalink)  
Old May 24th, 2006, 10:39 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
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
Reply With Quote
  #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 :)

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 03:18 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.