Answer the question
In order to leave comments, you need to log in
Why can't XSL transform be performed on a dynamically constructed DOMDocument instance?
Hello.
Haven't worked with XSL for a long time, especially with its PHP implementations.
There was a task to convert a certain document, based on dynamic data coming from outside in the format of an associative php array.
This is the XSL style I expect as output:
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="from">
<to>
<xsl:apply-templates/>
</to>
</xsl:template>
</xsl:stylesheet>
$xsldoc = new DOMDocument('1.0', 'utf-8');
$root = $xsldoc->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:stylesheet');
$first_tpl = $xsldoc->createElement('xsl:template');
$first_tpl->setAttribute('match', 'node()|@*');
$copy = $xsldoc->createElement('xsl:copy');
$inner = $xsldoc->createElement('xsl:apply-templates');
$inner->setAttribute('select', 'node()|@*');
$copy->appendChild($inner);
$first_tpl->appendChild($copy);
$root->appendChild($first_tpl);
$root->setAttribute('version', '1.0');
$xsldoc->appendChild($root);
// и еще несколько строк генерации дерева, опущу их для краткости
$proc = new XSLTProcessor();
$proc->importStylesheet($xsldoc);
var_dump($proc->transformToXML($this->document));
PHP Warning: XSLTProcessor::importStylesheet(): Found a top-level element xsl:template with null namespace URI
$proc = new XSLTProcessor();
$n = new DOMDocument();
$n->loadXML($xsldoc->saveXML());
$proc->importStylesheet($xsldoc);
var_dump($proc->transformToXML($this->document)); // здесь всё отлично
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question