This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: special chars in xsl:attribute tag


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, "&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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]