开发者

Pass PHP DOM to XSLT

I'm trying to load XML in a PHP document and pass it to an XSLT. I'm able to load the XML file directly into the XSLT, but I'm trying to avoid using actual files and instead just use cURL to get my data.

Here's my PHP script:

<?php

header("Content-type: text/xml");

$ch = curl_init("domain1.com/sample1.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_P开发者_运维知识库OST, 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");

?>

Here's the line from my XSLT file that uses the second file (sample2.xml):

<xsl:variable name="input2" select="document('sample2.xml')/DATA"/>

I know you can call some PHP functions within XLST, but I can't seem to figure out how to pass the XML DOMDocument directly to XSLT without first saving to a file. Is this possible? Or do I need to do this a different way? I'm pretty new to PHP and XSLT so any help is appreciated.

Thanks.


This is a possible solution - it won't work for all situations for instance when you need to make a POST to retrieve the XML, however it prevents having to save any XML files out. Let the XSLT processor perform the retrieval for you, and you can specify the files using the setParameter method on the processor.

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

Within the XSL you can, if appropriate, define default sources for your sample, which can be relative paths within the local filesystem or URIs.

<?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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜