Re: [xsl] & in attribute value template

Subject: Re: [xsl] & in attribute value template
From: Mike Brown <mike@xxxxxxxx>
Date: Sat, 19 Oct 2002 01:34:32 -0600 (MDT)
Guy McArthur wrote:
> Let's say I create an element like this:
> <a href="{@href}">...</a>
> And the value of href is something like "http://host?q=a&amp;u=v";.

The href value is actually "http://host/?q=a&u=v"; (assuming you meant to put
the slash after the host)... In the serialized document, pre-parsing, it has
"&amp;", but as far as the XSLT processor is concerned, string(@href) has "&"
in it.
> Well, the actual output is <a href="http://host?q=a&u=v";>...</a> which 
> does not validate.

No processor outputting XML or HTML should do that. When using <xsl:output
method="html"/> or <xsl:output method="xml"/>, it's supposed to escape the "&"
when it outputs the attribute value.

> The solution is to write ampersands in the URL as &amp;amp;

Yowch. Your XSLT processor is broken. In any other processor, you'd get
"&amp;amp;" in your output if you did that.

What XSLT processor are you using, exactly?

And if you can't upgrade it to one that's conformant, I'd see about wrapping
it with something like

  <xsl:attribute name="href">
      <xsl:when test="system-property('xsl:vendor')='Crappy XSLT Makers'">
        <!-- see for 'replace' template -->
        <xsl:call-template name="replace">
          <xsl:with-param name="stringIn" select="@href"/>
          <xsl:with-param name="substringIn" select="'&amp;'"/>
          <xsl:with-param name="substringOut" select="'&amp;amp;'"/>
        <xsl:value-of select="@href"/>

lest it come back to haunt you.

   - Mike
  mike j. brown                   |  xml/xslt:
  denver/boulder, colorado, usa   |  resume:

 XSL-List info and archive:

Current Thread