3
我試圖在PHP文檔中加載XML並將其傳遞給XSLT。我可以直接將XML文件加載到XSLT中,但我試圖避免使用實際文件,而只是使用cURL來獲取我的數據。將PHP DOM傳遞給XSLT
這裏是我的PHP腳本:
<?php
header("Content-type: text/xml");
$ch = curl_init("domain1.com/sample1.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$sample1 = new DOMDocument();
$sample1->formatOutput = true;
$sample1->loadXML($output);
$sample1->save("sample1.xml");
$ch = curl_init("domain1.com/sample2.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$sample2 = new DOMDocument();
$sample2->formatOutput = true;
$sample2->loadXML($output);
$sample2->save("sample2.xml");
/* load the xml file and stylesheet as domdocuments */
$xsl = new DomDocument();
$xsl->load("transform.xsl");
/* create the processor and import the stylesheet */
$proc = new XsltProcessor();
$xsl = $proc->importStylesheet($xsl);
$mergedxml = new DomDocument();
$mergedxml->loadXML($proc->transformToXML($sample1));
echo $mergedxml->saveXML();
$mergedxml->save("results.xml");
?>
下面是我的XSLT文件使用第二個文件(sample2.xml)行:
<xsl:variable name="input2" select="document('sample2.xml')/DATA"/>
我知道你可以在調用一些PHP函數XLST,但我似乎無法弄清楚如何在不首先保存到文件的情況下將XML DOMDocument直接傳遞給XSLT。這可能嗎?或者我需要以不同的方式做到這一點?我對PHP和XSLT相當陌生,所以對我們的幫助表示讚賞。
謝謝。
根據http://php.net/manual/en/xsltprocessor.setparameter.php,沒有辦法做到這一點,但我也很好奇。 – Tomalak
@Tomalak - 是的,我也看到了。希望有另一種方式來做到這一點。 – jared