XSL - How to disable output escaping for an attribute?
Asked Answered
B

2

11

I've had the following <a> tag:

<a href="http://myserver/_forms?url={@FileRef}&amp;id=5">...</a>

One of the files is called "File's got apostrophe.xml". The output of the XSL is:

<a href="http://myserver/_forms?url=/blah/File&amp;#39;s got apostrophe.xml&id=5">...</a>

The problem is that the apostrophe is HTML-escaped (twice?) into &amp;#39;, which breaks the link.

I've also tried using <xsl:attribute>, with same results:

<a>
  <xsl:attribute name="href">
    <xsl:value-of select="concat('http://myserver/_forms?url=', @FileRef, '&amp;id=5')"
         disable-output-escaping="yes" />
  </xsl:attribute>
</a>

Outputting <xsl:value-of select="@FileRef" disable-output-escaping="yes" /> works well - the unescaped value is printed on the page.

How can I set the attribute without escaping the string?

Billmyre answered 27/5, 2010 at 12:38 Comment(4)
have you tried storing the string to a variable and outputting that ?Weapon
have you tried different xslt-processors/browsers?Vistula
@Gaby - I didn't try that, I thought of it, but it didn't make sense. It didn't seem better than outputting the @FileRef field. Are you referring the the whole <a> tag, similar to tpeczek's answer?Billmyre
@fx42 - No I haven't. It might work differently on another processor, but since I'm using SharePoint, I don't really get to choose.Billmyre
C
18

You can generate your <a> as text:

<xsl:text disable-output-escaping="yes">&lt;a href="</xsl:text>
<xsl:value-of select="concat('http://myserver/_forms?url=', @FileRef, '&amp;id=5')" disable-output-escaping="yes" />
<xsl:text disable-output-escaping="yes">" &gt;/a&lt;</xsl:text>
Charley answered 27/5, 2010 at 12:50 Comment(2)
That's a pretty good idea, looks like this is going to work. I'll check on Sunday when I'm back to work. Thanks!Billmyre
That worked, though the result is exceptionally ugly. What a shame for that next developer. Thanks!Billmyre
P
4

I know I'm a bit late on this, but I think the attribute tag is the way to, you just don't want to concat...

<a>
  <xsl:attribute name="href">
    http://myserver/_forms?url=<xsl:value-of select="@FileRef" disable-output-escaping="yes" />&amp;id=5
  </xsl:attribute>
</a>
Pothouse answered 27/5, 2011 at 1:17 Comment(2)
I had this problem : #7887516Peptic
(source xslt specs:)It is an error for output escaping to be disabled for a text node that is used for something other than a text node in the result tree. Thus, it is an error to disable output escaping for an xsl:value-of or xsl:text element that is used to generate the string-value of a comment, processing instruction or attribute node;Peptic

© 2022 - 2024 — McMap. All rights reserved.