Using text () to match custom entity names in XSLT
I use <xsl:template match="m:*/text()">to match text in my XML document, which works great for plain text and famous entities, i.e. works great for objects like &or unicode like π.
However, what does not work corresponds to custom entity names. For example, I have an object πin my XML document that needs to be mapped using text(). For some reason, he does not consider this entity as a text, that is, nothing is matched.
Note that I declared the name of the object in the Doctype declaration of the XML document and XSLT document:
<!DOCTYPE xsl:stylesheet [<!ENTITY pi "π">]>
Is the text()correct approach to matching user entity names, or do I need to use another function? (Perhaps I also did something wrong by declaring the name of the object?)
thank
Edit
XML
<!DOCTYPE mathml [<!ENTITY pi "π">]>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline">
<mi>π</mi>
<mi>test</mi>
<mi>π</mi>
</math>
XSLT
<?xml version='1.0' encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [<!ENTITY pi "π">]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m="http://www.w3.org/1998/Math/MathML"
version='1.0'>
<xsl:template match="m:*/text()">
<xsl:call-template name="replaceEntities">
<xsl:with-param name="content" select="normalize-space()"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="replaceEntities">
<xsl:param name="content"/>
<xsl:value-of select="$content"/>
</xsl:template>
</xsl:stylesheet>
The variable $contentshould be printed three times, but only testand are printed π.
PHP Processing
$xslDoc = new DOMDocument();
$xslDoc->load("doc.xsl");
$xslProcessor = new \XSLTProcessor();
$xslProcessor->importStylesheet($xslDoc);
$mathMLDoc = new DOMDocument();
$mathMLDoc->loadXML('<!DOCTYPE mathml [<!ENTITY pi "π">]><math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mi>π</mi><mi>test</mi><mi>π</mi></math>');
echo $xslProcessor->transformToXML($mathMLDoc);
As far as I can see, the problem is that the DTD does not appear in the XSLT stylesheet. To convert objects with their text value, use the following:
$mathMLDoc->substituteEntities = true;
how in
$xslDoc = new DOMDocument();
$xslDoc->load("tree.xsl");
$xslProcessor = new \XSLTProcessor();
$xslProcessor->importStylesheet($xslDoc);
$mathMLDoc = new DOMDocument();
$mathMLDoc->substituteEntities = true;
$mathMLDoc->loadXML('<!DOCTYPE math [<!ENTITY pi "π">]><math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mi>π</mi><mi>test</mi><mi>π</mi></math>');
echo $xslProcessor->transformToXML($mathMLDoc);
which will produce
<?xml version="1.0"?>
ฯtestฯ
: http://php.net/manual/en/xsltprocessor.transformtoxml.php#99932 http://hublog.hubmed.org/archives/001854.html.