RE: [xsl] special characters replaced in javascript url

Subject: RE: [xsl] special characters replaced in javascript url
From: "Mingqiang Yu" <myu@xxxxxxxx>
Date: Sat, 22 Oct 2005 16:58:31 -0600 (MDT)
Thanks for the help. I tried the last way (use attributes such as onClick
) and things work fine now. Here is the piece of code I used. Hope it will
help someone who got similar problems.

<xsl:element name='a'>
<xsl:attribute name='href'>#</xsl:attribute>
<xsl:attribute name='onClick'>
 <xsl:text>frLink('FRLink','</xsl:text><xsl:value-of select="
$field"/><xsl:text>',&quot;</xsl:text><xsl:value-of
select="$thisTerm"/><xsl:text>&quot;)</xsl:text>
</xsl:attribute>
 <xsl:value-of select="."/>
</xsl:element>

Instead of:

<a href="javascript:frLink('FRLink','{$field}',&quot;{$thisTerm}&quo
t;);"><xsl:copy-of select="."/></a>

Thanks a lot for your help.

Ming

> Non-ASCII characters cannot legally appear in a URI, which is why XSLT
> always applies %-escaping to them. Unfortunately browsers aren't always
> very
> compliant with the specs, and this is particularly the case with
> Javascript
> "URLs" which usually are not really URIs at all.
>
> In XSLT 2.0 you can suppress the automatic %-escaping of URI attributes
> (Saxon 6.5.x also has an extension to do it with 1.0). With other
> processors, the best rule is to keep the Javascript in your href
> attributes
> very simple, just invoking functions defined in a script section. Better
> still, don't use href at all, instead use attributes such as onClick which
> are not defined as URIs.
>
> Michael Kay
> http://www.saxonica.com/
>
>> -----Original Message-----
>> From: Mingqiang Yu [mailto:myu@xxxxxxxx]
>> Sent: 22 October 2005 05:59
>> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
>> Subject: [xsl] special characters replaced in javascript url
>>
>> I have a problem which was asked a while ago but I couldn't
>> find the final
>> answer to that question. So I'm sorry that I have to raise
>> this question
>> again.(http://lists.xml.org/archives/xml-dev/200310/msg00841.html)
>>
>> I have a very similar problem in which, I have an xml record
>> passed to my
>> stylesheet which contains the following string:
>> Cr&eacute;mieu, V.
>> So, in order for the stylesheet to handle this special characters
>> correctly, I need to defined it in my dtd file(document.dtd)
>> like this:
>> <!ENTITY eacute  "&#233;" >
>> and include this dtd file in the xml file passed to the stylesheet:
>> <!DOCTYPE document  SYSTEM "document.dtd" >
>>
>> Then, the special characters are displayed correctly but when
>> I tried to
>> pass it as part of the url through javascript:
>> <a
>> href="javascript:frLink('FRLink','author',&quot;{$thisTerm}&quot;);">
>> in which the $thisTerm is the string Cr&eacute;mieu, V., the
>> same string
>> becomes "Cr%C3%A9mieu, V".
>>
>> I tried to use the solution introduced in the link above by doing
>> escape-javascript like this:
>> <xsl:template name="escape-javascript">
>>     <xsl:param name="string" />
>>     <xsl:choose>
>>         <xsl:when test='contains($string, "&#233;")'>
>>             <xsl:call-template name="escape-javascript">
>>                 <xsl:with-param name="string"
>> select='substring-before($string, "&#233;")' />
>>             </xsl:call-template>
>>             <xsl:text>&amp;eacute;</xsl:text>
>>             <xsl:call-template name="escape-javascript">
>>                 <xsl:with-param name="string"
>> select='substring-after($string, "&#233;")' />
>>             </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>
>>
>> It seems to work for this case. But I have too many special characters
>> that I cannot do the escape for every special characters.
>>
>> Can you help to find a better solution to solve this problem?
>>
>> Thanks in advance.
>>
>> Ming

Current Thread