# XSL and javascript question

**URL:** <https://boards.straightdope.com/t/xsl-and-javascript-question/232696>\
**Category:** Factual Questions\
**Created:** [March 5, 2004, 2:15pm UTC](https://boards.straightdope.com/t/xsl-and-javascript-question/232696 "2004-03-05T14:15:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bruce\_Daddy](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@Bruce\_Daddy](https://boards.straightdope.com/u/Bruce_Daddy)\
**Post date:** [March 5, 2004, 2:15pm UTC](https://boards.straightdope.com/t/xsl-and-javascript-question/232696/1 "2004-03-05T14:15:53Z")

</div>

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? 😕

---

<div class="post-metadata">

**Author:** ![Armilla](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Armilla](https://boards.straightdope.com/u/Armilla)\
**Post date:** [March 5, 2004, 2:26pm UTC](https://boards.straightdope.com/t/xsl-and-javascript-question/232696/2 "2004-03-05T14:26:24Z")

</div>

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:

```auto

       <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:

```auto

<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.

---

<div class="post-metadata">

**Author:** ![Bruce\_Daddy](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@Bruce\_Daddy](https://boards.straightdope.com/u/Bruce_Daddy)\
**Post date:** [March 5, 2004, 2:54pm UTC](https://boards.straightdope.com/t/xsl-and-javascript-question/232696/3 "2004-03-05T14:54:48Z")

</div>

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):

```auto

<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.

---

<div class="post-metadata">

**Author:** ![Bruce\_Daddy](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@Bruce\_Daddy](https://boards.straightdope.com/u/Bruce_Daddy)\
**Post date:** [March 5, 2004, 2:57pm UTC](https://boards.straightdope.com/t/xsl-and-javascript-question/232696/4 "2004-03-05T14:57:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Armilla](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Armilla](https://boards.straightdope.com/u/Armilla)\
**Post date:** [March 5, 2004, 3:02pm UTC](https://boards.straightdope.com/t/xsl-and-javascript-question/232696/5 "2004-03-05T15:02:18Z")

</div>

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:

```auto

<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.

---

<div class="post-metadata">

**Author:** ![Bruce\_Daddy](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@Bruce\_Daddy](https://boards.straightdope.com/u/Bruce_Daddy)\
**Post date:** [March 5, 2004, 3:21pm UTC](https://boards.straightdope.com/t/xsl-and-javascript-question/232696/6 "2004-03-05T15:21:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Armilla](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Armilla](https://boards.straightdope.com/u/Armilla)\
**Post date:** [March 5, 2004, 3:59pm UTC](https://boards.straightdope.com/t/xsl-and-javascript-question/232696/7 "2004-03-05T15:59:16Z")

</div>

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](http://www.dpawson.co.uk/xsl/sect2/sect21.html) site. I’ve found many a useful tip there.
