Subject: Re: [xsl] special chars in xsl:attribute tag From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx> Date: Sat, 17 Aug 2002 10:55:04 +0100 |
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
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] special chars in xsl:attribut, Doug Howell | Thread | [xsl] Temp Turn-Off of xsl List, Klass,Joel |
Re: [xsl] DOM and XML parser, Jeni Tennison | Date | Re: [xsl] Merge - a.xml + b.xml = c, Carsten Klein |
Month |