XSL and javascript question

I’ve got a button in a XSL template that is supposed to call a delete function:

<a href=“javascript:deleteLINE(’{WHATEVER}’)”>
<img src=“icons/trash.gif” border=“0”/>
</a>

That’s all well and good except sometimes WHATEVER has a ’ in it. For instance, “Bruce’s sucky code”.

This turns the button function into

deleteLINE(‘Bruce’s sucky code’);

with 3 apostropies, which is no good.

I’ve googled my ass off and cant find how you escape out or even replace the ’ with %27 or /’.

Help? :confused:

One option would be to use something else as the key when referring to the line to be deleted. Perhaps the generate-id() could be used to generated a unique ID for each line?

If that’s not useful I’m pasting a recursive find & replace template in at the bottom of this post. This can be used to replace specific characters or strings in a value with others. However, since it uses recursion, it’s not terribly efficient and it’s main advantage is that it only requires vanilla XSL 1.0.

If you’re using MSXML (or any other parser that supports scripting functions) then I’d recommend inserting a VB Script function in an msxsl:script block that calls the Replace function and returns the result. This is rather more efficient, but will tie the stylesheet to the specific parser you’re using.

Anyway: The recursive find & replace template is:



       <xsl:template name="replace">
              <xsl:param name="input"/>      <!-- Input string to search -->
              <xsl:param name="find"/>       <!-- Character(s) to find -->
              <xsl:param name="replace"/>    <!-- Replacement string -->
              <xsl:choose>
                     <!-- Input contains one or more instances of $find -->
                     <xsl:when test="contains($input,$find)">
                            <!-- Render everything before the first occurrance of $find -->
                            <xsl:value-of select="substring-before($input,$find)"/>
                            <!-- Apply this template to the remainder of the string -->
                            <xsl:value-of select="$replace" disable-output-escaping="yes"/>
                            <xsl:call-template name="replace">
                                   <xsl:with-param name="input"><xsl:value-of select="substring-after($input,$find)"/></xsl:with-param>
                                   <xsl:with-param name="find"><xsl:value-of select="$find"/></xsl:with-param>
                                   <xsl:with-param name="replace"><xsl:value-of select="$replace"/></xsl:with-param>
                            </xsl:call-template>
                     </xsl:when>
                     <!-- Input does not contain any instances of $find -->
                     <xsl:otherwise>
                            <!-- Just write the entire string unmodified -->
                            <xsl:value-of select="$input"/>
                     </xsl:otherwise>
              </xsl:choose>
       </xsl:template>


Try something like:



<xsl:call-template name="replace">
	<xsl:with-param name="input">Bruce's sucky code</xsl:with-param>
	<xsl:with-param name="find">'</xsl:with-param>
	<xsl:with-param name="replace">/'</xsl:with-param>
</xsl:call-template>


Hope this helps.

That works beautifully, thank you! The problem is I don’t know how to get that value into my javascript call. Code now looks like this (the real thing):


<xsl:variable name="rCARE_PHRASE select=call-template name="replace">
	<xsl:with-param name="input"><xsl:value-of select="CARE_PHRASE"/></xsl:with-param>
	<xsl:with-param name="find">'</xsl:with-param>
	<xsl:with-param name="replace">/'</xsl:with-param>
</xsl:call-template>

            <a href="javascript:deleteLINE('{CARE_CODE}','{PHRASE_CODE}','{LANGUAGE}','{$rCARE_PHRASE}','{$passSRC}')">
               <img src="icons/trash.gif" border="0"/>
            </a>


I know that’s wrong, but am I close? How do I get the “replaced” CARE_CODE into the script?
Sorry for the simpleton questions, I’m very new to XSL.

Oops. Left out the </xsl:variable>. Still is wrong though.

If you want to assign the replaced value to a variable you just need to call the template inside the xsl:variable element. Variables can take their value either from a select attribute or from the contents of the element:



<xsl:variable name="rCARE_PHRASE">
	<xsl:call-template name="replace">
		<xsl:with-param name="input">Bruce's sucky code</xsl:with-param>
		<xsl:with-param name="find">'</xsl:with-param>
		<xsl:with-param name="replace">/'</xsl:with-param>
	</xsl:call-template>
</xsl:variable>


You shouldn’t have any trouble with white space bleeding in like this, but in the case that you do edit it so that everything from <xsl:variable… to </xsl:variable> is on one line.

Did I ever tell you you’re my hero? You’re everything I would like to be. . .

Glad I could help. Feel free to email me if you have any more XSL related grief.

As an aside, one of the best XSL reference sites is the XSL-List FAQ site. I’ve found many a useful tip there.