xsl-list
[Top] [All Lists]

Re: & in attribute value template

2002-10-19 00:34:32
Guy McArthur wrote:
Let's say I create an element like this:

<a href="{(_at_)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



<Prev in Thread] Current Thread [Next in Thread>