I am using XSLT to extract some HTML content with special characters (e.g. ) from an XML file. Content is stored in <content> nodes. I defined most special characters as follows: <!ENTITY nbsp " "> , so this expression works fine:
<xsl:copy-of select="content" disable-output-escaping="yes"/>
Now I want to add target="_blank" to every link found in this content. This is the solution I came across:
<xsl:template match="a" mode="html"> <a> <xsl:attribute name="href"><xsl:value-of select="@*"/></xsl:attribute> <xsl:attribute name="target">_blank</xsl:attribute> <xsl:apply-templates select="text()|* "/> </a> </xsl:template>
And instead of the copy-of element, I use this:
<xsl:apply-templates select="content" mode="html"/>
Now all these special characters (and nbsp too) have disappeared from the output. How to save them? It seems that disable-output-escaping="yes" doesn't help here.
Ok, I'm using the XSLTProcessor class in PHP. The disable-output-escaping attribute did not actually give an error, but when I deleted it, the result was the same with all nbsp, so it didn't matter.
UPD With the XSL template that I showed earlier, my input example:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE page SYSTEM "html-entities.xsl"> <content>There is a non-breaking <a href="http://localhost">space</a> inside.</content>
HTML-entities.xsl:
<?xml version="1.0" encoding="UTF-8"?> <!ENTITY nbsp " ">
PHP code:
$xp = new XSLTProcessor(); $xsl = new DOMDocument(); $xsl->load($xsl_filename); $xp->importStylesheet($xsl); $xml_doc = new DOMDocument(); $xml_doc->resolveExternals = true; $xml_doc->load($xml_filename); $html = $xp->transformToXML($xml_doc);
My current output is:
There is anon-breaking <a href="http://localhost" target="_blank">space</a> inside.
My desired result:
There is a non-breaking <a href="http://localhost" target="_blank">space</a> inside.