[xsl] RE: special chars in xsl:attribute tag

Subject: [xsl] RE: special chars in xsl:attribute tag
From: Doug Howell <doug@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 27 Aug 2002 13:11:58 -0400
I wrote:
> 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.

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

Even when Jeni helped me fix the escaping problem, I still had the problem
of sending the escaped search term back to the stylesheet to search for the
correct XML term. Thanks to Jeni's on- and offline advice, I fixed the
problem and created a much tighter application by reconfiguring it as a
stylesheet template with parameters that can be passed in to set the
transformation mode and variables. Just thought I'd post this in case anyone
else is struggling in JavaScript escaping and unescaping hell.

XML now looks like this:
<glossary>
    <entry>
        <term cat="sw">Beginner&#8217;s All-Purpose Symbolic Instruction
Code</term><!-- term includes right single quote  -->
        <acronym>Basic</acronym>
        <def>Programming language using common English words. Developed in
the mid-1960s, it is widely considered one of the easiest programming
languages to learn.</def>
        <see-also>Visual Basic</see-also>
        <src type="printed-doc">MS Press Computer Dictionary</src>
        <ed>ddh</ed>
    </entry>
</glossary>

XSLT now looks like this:
<!-- the following template processes each term or acronym for display -->
<xsl:template match="term|acronym" name="term_acronym">
    <p><strong><xsl:value-of select="."/></strong>:
    <xsl:if test="name()='acronym'">
        <em>See </em><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>
    </xsl:if>
    <xsl:if test="name()='term'">
        <xsl:if test="following-sibling::acronym">
            (<xsl:value-of select="following-sibling::acronym"/>)
        </xsl:if>
        <xsl:apply-templates select="following-sibling::def"/>
        <xsl:apply-templates select="following-sibling::see-also"/>
        <xsl:apply-templates select="following-sibling::compare"/>
        <xsl:apply-templates select="following-sibling::src"/>
        <xsl:apply-templates select="following-sibling::example"/>
    </xsl:if>
    </p>
</xsl:template>

<xsl:template name="escape-javascript">
    <xsl:param name="string" />
    <xsl:choose>
        <xsl:when test='contains($string, "&#8217;")'>
            <xsl:call-template name="escape-javascript"><xsl:with-param
name="string" select='substring-before($string, "&#8217;")'
/></xsl:call-template>
            <xsl:text>\u2019</xsl:text>
            <xsl:call-template name="escape-javascript"><xsl:with-param
name="string" select='substring-after($string, "&#8217;")'
/></xsl:call-template>
        </xsl:when>
        <!-- test for other special characters or entities here -->
        <xsl:otherwise><xsl:value-of select="$string" /></xsl:otherwise>
    </xsl:choose>
</xsl:template>

HTML JavaScript now looks like this:
var src, ss, cache;
...
[load XML file into src]
[load stylesheet into ss]
[create template and store it in cache]
...
function searchExact(searchTerm)
{
    var processor = cache.createProcessor();
    processor.input = src;
    processor.setStartMode("exact");
    processor.addParameter("searchTerm", searchTerm);
    processor.transform();
    glossary.innerHTML = processor.output;
}


This combination works really well now, thanks!
Doug

--------------------------
Doug Howell
Information Architect
Borders Group Inc.


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread