How to output ampersand (&) from XSLT

我是研究僧i 提交于 2020-04-10 09:41:09

问题


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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/31437121/how-to-output-ampersand-from-xslt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!