XSL producing HTML can not insert HTML fragments f

2019-09-11 02:36发布

问题:

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


标签: xml xslt