Re: [xsl] url encoding of ampersands

Subject: Re: [xsl] url encoding of ampersands
From: Mike Brown <mike@xxxxxxxx>
Date: Sun, 25 Feb 2001 12:36:17 -0700 (MST)
Sivan Mozes wrote:
> Description: 
> A link is pointing to an anchor, whose name contains an
> accented character. The URL is constructed based on a text node in the
> XML to avoid using special characters in an attribute. Next, the
> stylesheet needs to replace all ampersands with %26 for URL
> encoding.
> Example:
> XML:
> <link type="anchor">Cr&#233;pe</link>
> Expected result:
> <a href="#Cr%26#233;pe">Cr&#233;pe</a>

According to
you really want:

<a href="#Cr%C3%A9pe">Cr&#233;pe</a>
<a href="#Cr%C3%A9pe">Cr&eacute;pe</a>

Though if %C3%A9 doesn't work, and if your output encoding is iso-8859-1 or
windows-1252, try %E9.

> Template:
> <xsl:template match="link">
>   <xsl:variable name="anchor" select="concat(substring-before( . ,
> '&amp;'), '%26', substring-after( . , '#'))"/>

This won't work because there is no ampersand in your *source tree* (which
was *derived* from your original XML). &#233; in your XML is resolved by
the XML parser into a single LATIN SMALL E WITH ACUTE character long before
the XSLT processor sees it. Likewise, your output will be *derived* from
the result tree you construct, hence the automatic conversion of the
character in question into &eacute; or &#233; by the XSLT processor's
result tree serializer, just as it would convert an ampersand into &amp;.

<xsl:variable name="anchor" select="concat(substring-before(.,'&#233;'),

   - Mike
Mike J. Brown, software engineer at            My XML/XSL resources: in Denver, Colorado, USA    

 XSL-List info and archive:

Current Thread