我正在尝试包含不同的源文件(例如 file1.xml 和 file2.xml),并为使用 PHPs 的 XSLT 转换解析这些包含XSLTProcessor
。这是我的输入:
源代码.xml
<?xml version="1.0" encoding="utf-8" ?>
<root xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="file1.xml" />
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="file2.xml" />
</root>
变换.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xi="http://www.w3.org/2001/XInclude">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
</xsl:transform>
转换.php
<?php
function transform($xml, $xsl) {
global $debug;
// XSLT Stylesheet laden
$xslDom = new DOMDocument("1.0", "utf-8");
$xslDom->load($xsl, LIBXML_XINCLUDE);
// XML laden
$xmlDom = new DOMDocument("1.0", "utf-8");
$xmlDom->loadHTML($xml); // loadHTML to handle possibly defective markup
$xsl = new XsltProcessor(); // create XSLT processor
$xsl->importStylesheet($xslDom); // load stylesheet
return $xsl->transformToXML($xmlDom); // transformation returns XML
}
exit(transform("source.xml", "transform.xsl"));
?>
我想要的输出是
<?xml version="1.0" encoding="utf-8" ?>
<root>
<!-- transformed contents of file1.xml -->
<!-- transformed contents of file2.xml -->
</root>
我当前的输出是我的源文件的精确副本:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="file1.xml" />
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="file2.xml" />
</root>