XSL producing HTML can not insert HTML fragments from XML

人盡茶涼 提交于 2020-01-16 18:11:26

问题


What I want to achieve:

1 XSL file produces an HTML output based on XSLT. Parts of the output HTML is stored in XML. So you need to read that part from an XML and insert it (merge) with the other content that XSL directly produces.

Example Code XSL:

<div>
  <h1>This is a title</h1>
  <p>And this is the content coming from XML:<br/>
    <xsl:copy-of select="/*/xhtml:myelement" />
  </p>
</div>

And the XML looks like this:

<?xml version="1.0" encoding="UTF-8"?>
  <z:article xmlns:ns0="http://www.w3.org/1999/xhtml">
  <ns0:myelement>&lt;p>here is some text with a special character &amp;#776; and &amp;#187; but actually any valid html can be stored like this in XML.&lt;/p>&lt;br/>  
    &lt;h2>an H2 title in the XML&lt;/h2>
    &lt;p>and again some text&lt;/p> 
  </ns0:myelement>
</z:article>

What I get as output is the HTML itself (as text), so it is still escaped and therefore not interpreted as HTML:

This is a title

And this is the content coming from XML:
<p>here is some text with a special character &#776; and &#187; but actually any valid html can be stored like this in XML.</p> <h2>an H2 title in the XML</h2> <p>and again some text</p>

So my question is: HOW can I write the XSL code so that the text in the XML is copied as actual HTML and not as TEXT ?


回答1:


Instead of:

<xsl:copy-of select="ns0:myelement" />

try:

<xsl:value-of select="ns0:myelement" disable-output-escaping="yes"/>


来源:https://stackoverflow.com/questions/25508777/xsl-producing-html-can-not-insert-html-fragments-from-xml

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