简体   繁体   English

PHP中的多个XML / XSLT文件,使用XSLT转换一个文件并添加其他文件,但首先使用PHP处理它

[英]Multiple XML/XSLT files in PHP, transform one with XSLT and add others but process it first with PHP

I am processing XML files transformations with XSLT in PHP correctly. 我正在用PHP中的XSLT正确处理XML文件转换。 Actually I use this code: 实际上,我使用以下代码:

$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_contents is the XML processed with PHP, this is done by including the XML file first and then assigning $xml_contents = ob_get_contents(); ob_end_clean(); $xml_contents是用PHP处理的XML,方法是先包含XML文件,然后分配$xml_contents = ob_get_contents(); ob_end_clean(); $xml_contents = ob_get_contents(); ob_end_clean(); . This forces to process the PHP code on the XML, and it works perfectly. 这迫使在XML上处理PHP代码,并且效果很好。

My problem is that I use more than one XML file and this XML files has PHP code on it that need to be processed AND have a XSLT file associated to process the data. 我的问题是我使用了多个XML文件,并且此XML文件上有需要处理的PHP代码,并且具有关联的XSLT文件来处理数据。 Actually I'm including this files in XSLT with the next code: 实际上,我将使用以下代码将这些文件包含在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" />

My questiion is how I can handle this. 我的问题是我该如何处理。 I need to add mutiple XML(+XSLT) files to my first XML file that will containt PHP so it needs to be processed. 我需要将多个XML(+ XSLT)文件添加到将包含PHP的第一个XML文件中,因此需要对其进行处理。

Thank you in advance! 先感谢您!

I'm not sure what you mean by "XML file that will contain PHP", but I'm guessing that you need php to interfere with the XSLT processing after it's started. 我不确定“将包含PHP的XML文件”是什么意思,但是我猜想在启动XSLT之后,您需要php来干扰它。 Otherwise you'd be using PHP to manipulate the source XML before any XSLT transformation is run. 否则,您将在运行任何XSLT转换之前使用PHP来操纵源XML。

There's php functionality that allows you to run arbitrary php inside XSLT stylesheet during processing, although it is an extension of the standard XSLT. 有php功能,虽然它是标准XSLT的扩展,但允许您在处理过程中在XSLT样式表中运行任意php。

There's more info and examples . 更多信息和示例

EDIT: 编辑:

On the second thought, if you simply need to specify what XML to load without hardcoding it into XSLT, document() function may use source XML nodes for names of the files to load. 第二种想法,如果您只需要指定要加载的XML而无需将其硬编码到XSLT中,则document()函数可以使用源XML节点作为要加载的文件的名称。 So you just generate names in XML, and then have document() read them for further reference in the template. 因此,您只需以XML生成名称,然后让document()读取它们以在模板中进一步参考。

2 solutions, maybe one of them will be ok. 2种解决方案,也许其中一种可以。

Not tested, but you can implement a custom stream in PHP. 未经测试,但是您可以在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;


?>

Solution n°2, register php functions in your xslt, and play with it : 解决方案n°2,在您的xslt中注册php函数,并使用它:

<?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);

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM