PHP中有多个XML/XSLT文件,使用XSLT转换其中一个文件并添加其他文件,但首先使用PHP处理
我正在用PHP中的XSLT正确处理XML文件转换。实际上,我使用的代码是:PHP中有多个XML/XSLT文件,使用XSLT转换其中一个文件并添加其他文件,但首先使用PHP处理,php,xml,xslt,Php,Xml,Xslt,我正在用PHP中的XSLT正确处理XML文件转换。实际上,我使用的代码是: $xml = new DOMDocument; $xml->LoadXML($xml_contents); $xsl = new DOMDocument; $xsl->load($xsl_file); $proc = new XSLTProcesoor; $proc->importStyleSheet($xsl); echo $proc->transformToXml($xml); $xm
$xml = new DOMDocument;
$xml->LoadXML($xml_contents);
$xsl = new DOMDocument;
$xsl->load($xsl_file);
$proc = new XSLTProcesoor;
$proc->importStyleSheet($xsl);
echo $proc->transformToXml($xml);
$xml\u contents
是用PHP处理的xml,首先包括xml文件,然后分配$xml\u contents=ob\u get\u contents();ob_end_clean()代码>。这迫使我们在XML上处理PHP代码,而且它工作得非常好
我的问题是,我使用了多个XML文件,这些XML文件上有需要处理的PHP代码,并且有一个与处理数据相关联的XSLT文件。实际上,我在XSLT中包含了这些文件,下面的代码是:
<!-- First I add the XML file -->
<xsl:param name="menu" select="document('menu.xml')" />
<!-- Next I add the transformations for menu.xml file -->
<xsl:include href="menu.xsl" />
<!-- Finally, I process it on the actual ("parent") XML -->
<xsl:apply-templates select="$menu/menu" />
我的问题是如何处理这件事。我需要将多个XML(+XSLT)文件添加到第一个包含PHP的XML文件中,以便对其进行处理
提前谢谢你 我不确定您所说的“包含PHP的XML文件”是什么意思,但我猜您需要PHP在XSLT处理启动后干扰它。否则,您将在运行任何XSLT转换之前使用PHP操作源XML
php功能允许您在处理过程中在XSLT样式表中运行任意php,尽管它是标准XSLT的扩展
有一个
编辑:
再想一想,如果只需要指定要加载的XML而不将其硬编码到XSLT中,则document()函数可以使用源XML节点作为要加载的文件名。因此,您只需在XML中生成名称,然后让document()读取它们,以便在模板中进一步参考。2个解决方案,也许其中一个就可以了
未测试,但可以在PHP中实现自定义流
<?php
// define your custom stream, should process xslt
class CustomStream extends streamWrapper {
// http://www.php.net/manual/en/class.streamwrapper.php
}
// declare your custom stream
stream_wrapper_register('extra', 'CustomStream');
// then load your preprocessed on the fly in your example.xsl
$xml_contents = <<<EOF
...
<xsl:param name="menu" select="document('extra://menu.xml')" />
EOF;
?>
importStyleSheet($xsl);
返回$proc->transformToDoc($xml\u内容);
}
//然后处理主xsl
$xsl_contents=RegisterHPFunctions('resolve');
$proc->importStyleSheet($xsl);
返回$proc->transformToDoc($xml\u内容);
?>
您缺少流程链的一部分。如果我理解正确,那么这个链就是:XML+PHP->PHP pre-process->XSLT,您希望最后一步知道第一步中可以保存的相同数据。当您构建XML和经过修饰的PHP时,您知道要应用哪个样式表。那么,一定要运用它。@Alejandro:我不明白。我知道我将使用什么样的XML和XSLT,但我想添加额外的XML和XSLT,其中主XSLT中包含的XML可以用PHP预处理。因此,链将是:PHP控制器调用呈现“example.xml”->PHP加载xml并处理它,并自动关联“example.xslt”->打印输出。在与example.xslt关联的步骤中,这个example.xslt包含XML和xslt附加的各种include。我需要预先处理这些额外的XML文件。然后你需要重新考虑你的问题,因为现在还不清楚。
<?php
// declare your function
// see http://www.php.net/manual/fr/xsltprocessor.registerphpfunctions.php
function resolve($xml_contents, $xsl_file=null) {
$dom = new DomDocument;
$dom->loadXML($xml_contents);
if ($xsl == null) {
return $dom;
}
$xsl = new DOMDocument;
$xsl->load($xsl_file);
$proc = new XSLTProcessor;
$proc->registerPHPFunctions('resolve');
$proc->importStyleSheet($xsl);
return $proc->transformToDoc($xml_contents);
}
// then handle your master xsl
$xsl_contents = <<<EOF
...
<xsl:param name="menu" select="php:function('resolve', 'menu.xml', 'menu.xsl')" />
EOF;
$dom = new DomDocument;
$dom->loadXML($xml_contents);
$xsl = new DOMDocument;
$xsl->loadXML($xsl_contents);
$proc = new XSLTProcessor;
$proc->registerPHPFunctions('resolve');
$proc->importStyleSheet($xsl);
return $proc->transformToDoc($xml_contents);
?>