[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

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

<a>
  <xsl:attribute name="href">
    <xsl:choose>
      <xsl:when test="system-property('xsl:vendor')='Crappy XSLT Makers'">
        <!-- see http://skew.org/xml/stylesheets/replace/ 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:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="@href"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:attribute>
</a>

lest it come back to haunt you.

   - Mike
____________________________________________________________________________
  mike j. brown                   |  xml/xslt: http://skew.org/xml/
  denver/boulder, colorado, usa   |  resume: http://skew.org/~mike/resume/

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



Current Thread
Keywords