to match text in my XML document, which works great f...">

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 &amp;or unicode like &#x003C0;.

However, what does not work corresponds to custom entity names. For example, I have an object &pi;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 "&#x003C0;">]>

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 "&#x003C0;">]>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline">    
    <mi>&pi;</mi>
    <mi>test</mi>
    <mi>&#x003C0;</mi>
</math>

XSLT

<?xml version='1.0' encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [<!ENTITY pi "&#x003C0;">]>
<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 &#x003C0;.

PHP Processing

$xslDoc = new DOMDocument();
$xslDoc->load("doc.xsl");
$xslProcessor = new \XSLTProcessor();
$xslProcessor->importStylesheet($xslDoc);
$mathMLDoc = new DOMDocument();
$mathMLDoc->loadXML('<!DOCTYPE mathml [<!ENTITY pi "&#x003C0;">]><math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mi>&pi;</mi><mi>test</mi><mi>&#x003C0;</mi></math>');
echo $xslProcessor->transformToXML($mathMLDoc);
+4
source share
1 answer

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 "&#x003C0;">]><math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mi>&pi;</mi><mi>test</mi><mi>&#x003C0;</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.

+4

All Articles