[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: special chars in xsl:attribute tag

Subject: Re: special chars in xsl:attribute tag
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Sat, 17 Aug 2002 10:55:04 +0100
xsl attribute escape
Hi Doug,

> The trouble is with the apostrophe. If I include it as it appears above (or
> &#39;), 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, "&apos;")'>
      <xsl:call-template name="escape-javascript">
        <xsl:with-param name="string"
          select='substring-before($string, "&apos;")' />
      </xsl:call-template>
      <xsl:text>\'</xsl:text>
      <xsl:call-template name="escape-javascript">
        <xsl:with-param name="string"
          select='substring-after($string, "&apos;")' />
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="contains($string, '&#xA;')">
      <xsl:call-template name="escape-javascript">
        <xsl:with-param name="string"
          select="substring-before($string, '&#xA;')" />
      </xsl:call-template>
      <xsl:text>\n</xsl:text>
      <xsl:call-template name="escape-javascript">
        <xsl:with-param name="string"
          select="substring-after($string, '&#xA;')" />
      </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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.