Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output ampersand (&) from XSLT

Tags:

html

xml

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".

like image 513
davomarti Avatar asked Jul 15 '15 17:07

davomarti


People also ask

How do you pass ampersand in XML string?

Use & in place of & .

How do you handle special characters in XSLT?

It's very easy. Just extend the range of your translate() call to cover the problem characters (characters that you use in the attribute, but that XML forbids for element names).


2 Answers

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 &.

like image 127
kjhughes Avatar answered Oct 05 '22 17:10

kjhughes


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

like image 45
bbsimonbb Avatar answered Oct 05 '22 17:10

bbsimonbb