davomarti
davomarti

Reputation: 373

How to output ampersand (&) from XSLT

I'm converting all & into & in my XML so that the XSLT will compile. I'm styling the XML into HTML. However when a textbox is populated by the XSLT, I need the & to display as &.

For example, it shows "you & me" in the text box, but I need to see "you & me".

Upvotes: 8

Views: 34035

Answers (2)

bbsimonbb
bbsimonbb

Reputation: 28992

In many situations & will work very well. It's valid xsl, understood also by the browser. XSL, some-one-still-loves-youuuuuuuuu.

Upvotes: 6

kjhughes
kjhughes

Reputation: 111521

How to output & as & in XSLT

In general, here are alternative techniques for outputting & as &:

  • Globally:

    <xsl:output method="html"/>
    or
    <xsl:output method="text"/>`
    
  • For ampersands originating from XSLT:

    <xsl:text disable-output-escaping="yes"><![CDATA[&]]></xsl:text>
    
  • For ampersands originating from input XML:

    <xsl:value-of select="XPATH EXPRESSION" disable-output-escaping="yes"/>
    

Now, in your specific case you say that &amp;s within text boxes are being displayed as "&amp;". I don't see that at all. Apart from XML or XSLT, in which I show above how to generate & rather than &amp;, HTML itself really has no problem with &amp;...

Consider this simple test HTML:

<html>
  <body>
    <h3>No &amp;amp; is displayed in any of these cases:</h3>
    <div>
      In an input box:
      <input type="text" value="Ampersand: &amp;"/>
    </div>
    <div>
      In a text area:
      <textarea>Ampersand: &amp;</textarea>
    </div>
    <div>In a div: Ampersand: &amp;</div>
  </body>
</html>

This renders in browsers as follows:

enter image description here

As you can see, there is no problem rendering &amp; as &.

Upvotes: 14

Related Questions