xslt - Apply transforms to XML attribute containing escaped HTML -
i have xml looks this:
<?xml version="1.0" encoding="utf-8"?> <root> <issue xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <comment text="<div class="wiki text"><h4>tom fenech</h4>here comment</div> "/> </issue> </root>
as can see, text
attribute in comment
node contains escaped html. contents of attribute xhtml, inside template using:
<xsl:value-of select="@text" disable-output-escaping="yes" />
that gets me html in final output:
<div class="wiki text"><h4>tom fenech</h4>here comment</div>
but want able extract contents of <h4>
tag use elsewhere. in general, nice able manipulate contents of once has been escaped.
how apply further templates output of <xsl:value-of />
?
i using php built-in xslt processor, supports xslt version 1.0, although willing consider using alternative processor if features newer versions make possible.
here's 1 way it, calling php function xslt:
function parsehtmlstring($html) { $doc = new domdocument(); $doc->loadhtml($html); return $doc; } $xml = <<<eob <root> <issue xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <comment text="<div class="wiki text"><h4>tom fenech</h4>here comment</div> "/> </issue> </root> eob; $xsl = <<<eob <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:php="http://php.net/xsl" xsl:extension-element-prefixes="php"> <xsl:output method="html" encoding="utf-8" indent="yes"/> <xsl:template match="comment"> <xsl:apply-templates select="php:functionstring('parsehtmlstring', @text)//div/h4"/> </xsl:template> <xsl:template match="div/h4"> <h2><xsl:apply-templates/></h2> </xsl:template> </xsl:stylesheet> eob; $xmldoc = new domdocument(); $xmldoc->loadxml($xml); $xsldoc = new domdocument(); $xsldoc->loadxml($xsl); $proc = new xsltprocessor(); $proc->registerphpfunctions('parsehtmlstring'); $proc->importstylesheet($xsldoc); echo $proc->transformtoxml($xmldoc);
Comments
Post a Comment