This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: special chars in xsl:attribute tag
- From: Jeni Tennison <jeni at jenitennison dot com>
- To: Doug Howell <doug at creativemeasures dot com>
- Cc: xsl-list at lists dot mulberrytech dot com
- Date: Sat, 17 Aug 2002 10:55:04 +0100
- Subject: Re: [xsl] special chars in xsl:attribute tag
- Organization: Jeni Tennison Consulting Ltd
- References: <000601c24561$dcd2ec30$0200a8c0@dell4300>
- Reply-to: xsl-list at lists dot mulberrytech dot com
Hi Doug,
> The trouble is with the apostrophe. If I include it as it appears above (or
> '), the output is:
> <a href="javascript:searchExact('Beginner's All-Purpose Symbolic Instruction
> Code')">etc.
> This causes an error because the single-quotation mark comes too early.
Yep. In JavaScript, if you want to use a single quote in a string then
you have to escape it as \'. So you want to generate:
<a href="javascript:searchExact('Beginner\'s All-Purpose Symbolic
Instruction Code')">...</a>
There are various other escapes that you have to make for JavaScript
-- \ is escaped as \\; newlines have to be specified with \n and so
on. So you need to create a template that does this escaping, then
call your template when you create the attribute:
<a>
<xsl:attribute name="href">
<xsl:text>javascript:searchExact('</xsl:text>
<xsl:call-template name="escape-javascript">
<xsl:with-param name="string"
select="preceding-sibling::term" />
</xsl:call-template>
<xsl:text>')</xsl:text>
</xsl:attribute>
<xsl:value-of select="preceding-sibling::term" />
</a>
There are various methods for performing the replacements. Your
escape-javascript template might look something like:
<xsl:template name="escape-javascript">
<xsl:param name="string" />
<xsl:choose>
<xsl:when test='contains($string, "'")'>
<xsl:call-template name="escape-javascript">
<xsl:with-param name="string"
select='substring-before($string, "'")' />
</xsl:call-template>
<xsl:text>\'</xsl:text>
<xsl:call-template name="escape-javascript">
<xsl:with-param name="string"
select='substring-after($string, "'")' />
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($string, '
')">
<xsl:call-template name="escape-javascript">
<xsl:with-param name="string"
select="substring-before($string, '
')" />
</xsl:call-template>
<xsl:text>\n</xsl:text>
<xsl:call-template name="escape-javascript">
<xsl:with-param name="string"
select="substring-after($string, '
')" />
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($string, '\')">
<xsl:value-of select="substring-before($string, '\')" />
<xsl:text>\\</xsl:text>
<xsl:call-template name="escape-javascript">
<xsl:with-param name="string"
select="substring-after($string, '\')" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise><xsl:value-of select="$string" /></xsl:otherwise>
</xsl:choose>
</xsl:template>
for example.
Cheers,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list