3

I have the following xml-node :

<name>P &amp; P</name>

And following XSL

<a href="example.htm" >

    <xsl:attribute name="title">
       <xsl:value-of select="name" disable-output-escaping="yes"></xsl:value-of>
    </xsl:attribute>    


     <xsl:value-of select="name" disable-output-escaping="yes"></xsl:value-of>
</a>

That compiles to this HTML

<a href="example.com" title="P &amp;amp; P">
  P &amp; P
</a>

So the non-escaping worked for the value (the text between <A></A>) but not for the attribute.

Am I missing something here?

Thanks!

Peter
  • 47,963
  • 46
  • 132
  • 181

2 Answers2

2

From an OP's comment:

I need this xml (P & P) in the title attribute of an HTML tag. A better solution is most welcome!

What you need to generate can be done perfectly without D-O-E.

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
     <a href="example.htm" title="{.}">
       <xsl:value-of select="."/>
     </a>
 </xsl:template>
</xsl:stylesheet>

When applied on the following XML document:

<t>P &amp; P</t>

the wanted, correct result is produced:

<a href="example.htm" title="P &amp; P">P &amp; P</a>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • +1 : cause you are right. However, I simplified the example, and in my case sth. different is happening. I have to figure out what now. Tx. – Peter Oct 25 '11 at 14:38
  • @Peter: You have to provide a more precise example then. Your current question is completely answered. – Dimitre Novatchev Oct 25 '11 at 16:10
  • @Dimtitre, in fact the question intende was "why isn't output escaping working in xslt?" I guess the correct answer is :"cause it's designed that way" . Nevertheless, If I find time I will provide a precise example, otherwise, I'll happily accept your answer a bit later. Tx. for your trouble in any case. – Peter Oct 25 '11 at 16:43
  • @Peter: Yes, D-O-E is only defined within text nodes -- not attributes. My answer adresses your comment in which you stated what you actually wanted to achieve, and provides a D-O-E - less solution :) – Dimitre Novatchev Oct 25 '11 at 19:07
  • Since the answer 'no it's not possible' is kind of lame, I accept your answer as indeed an answer on my commant. I still hope if I find time to post a better sample you'll help me out too. – Peter Oct 25 '11 at 20:44
  • @Peter: Please, let me know via a comment when you have posted the new question -- I am glad to help. – Dimitre Novatchev Oct 25 '11 at 21:21
1

I've been looking around and I guess this is why : (if I understand correctly) ?

Out of the specs : (http://www.w3.org/TR/xslt)

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; it is also an error to convert a result tree fragment to a number or a string if the result tree fragment contains a text node for which escaping was disabled. In both cases, an XSLT processor may signal the error; if it does not signal the error, it must recover by ignoring the disable-output-escaping attribute.

So disabling output for escaping an attribute is just not possible apparantly. The workaround that I see is to build a string 'by hand' as XSL - How to disable output escaping for an attribute?

Still hard to believe that I'm not missing sth. trivial here.

Community
  • 1
  • 1
Peter
  • 47,963
  • 46
  • 132
  • 181
  • 2
    Correct, d-o-e doesn't work for attributes. Question is, why are you using d-o-e in the first place? There are a few valid use cases, but most of the time, it's an ugly hack and better solutions are possible. – Michael Kay Oct 25 '11 at 10:38
  • I need this xml (P & P) in the title attribute of an HTML tag. A better solution is most welcome! – Peter Oct 25 '11 at 11:37
  • No "to build a string 'by hand' " approach is necessary -- see my answer. :) – Dimitre Novatchev Oct 25 '11 at 14:35