2011-08-01 63 views
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相當陌生,所以對我們的幫助表示讚賞。

謝謝。

+0

根據http://php.net/manual/en/xsltprocessor.setparameter.php,沒有辦法做到這一點,但我也很好奇。 – Tomalak

+0

@Tomalak - 是的,我也看到了。希望有另一種方式來做到這一點。 – jared

回答

1

這是一個可能的解決方案 - 例如當您需要創建POST來檢索XML時,它不適用於所有情況,但它可以防止將任何XML文件保存出去。讓XSLT處理器爲您執行檢索,並且您可以使用處理器上的setParameter方法指定文件。

<?php 

/* load the xsl file into a DOMDocument, and set the documentURI */ 
$xsl = new DomDocument(); 
$xsl->load("transform.xsl"); 
$xsl->documentURI = "/path/to/transform.xsl"; 

/* need a document, can be empty */ 
$doc = new DOMDocument(); 

/* define the sources for your external XML, can be static or dynamic */ 
$params = array(
    'source-books' => 'http://example.com/books.php', 
    'source-films' => 'http://example.com/films.xml', 
); 

/* create the processor and import the stylesheet, set your source parameters */ 
$proc = new XsltProcessor(); 
$proc->importStylesheet($xsl); 
$proc->setParameter('',$params); 

echo $proc->transformToXml($doc); 

內XSL,你可以,如果合適的話,定義默認來源爲您的樣品,它可以是本地文件系統或URI中的相對路徑。

<?xml version="1.0" encoding="UTF-8"?> 

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" > 
    <xsl:output method="html" omit-xml-declaration="yes" /> 

    <!-- define default parameters --> 
    <xsl:param name="source-actors" select="'actors.xml'"/> 
    <xsl:param name="source-books" select="'http://example2/com/books.xml'"/> 

    <!-- pickup the source parameters --> 
    <xsl:variable name="films" select="document($source-films)/films"/> 
    <xsl:variable name="books" select="document($source-books)/books"/> 
    <xsl:variable name="actors" select="document($source-actors)/actors"/> 

    <xsl:template match="/"> 
    <html> 
     <body> 
     <h4> 
      Found <xsl:value-of select="count($films/film)" /> films in document at <xsl:value-of select="$source-films" /> 
     </h4> 
     <ul> 
      <xsl:apply-templates select="$films/film" /> 
     </ul> 
     <h4> 
      Found <xsl:value-of select="count($books/book)" /> books in document at <xsl:value-of select="$source-books" /> 
     </h4> 
     <ul> 
      <xsl:apply-templates select="$books/book" /> 
     </ul> 
     <h4> 
      Found <xsl:value-of select="count($actors/actor)" /> actors in document at <xsl:value-of select="$source-actors" /> 
     </h4> 
     <ul> 
      <xsl:apply-templates select="$actors/actor" /> 
     </ul> 
     </body> 
    </html> 
    </xsl:template> 

    <xsl:template match="film|book|actor"> 
     <li><xsl:value-of select="@uid" />: <xsl:value-of select="." /></li> 
    </xsl:template> 
</xsl:stylesheet>