简体   繁体   中英

save XSL output to XML with PHP

I have the following PHP code. The XSL transformation works fine, and echoes a string fine to the server, but when I try to save, I get "Fatal error: Call to undefined method stdClass::save()" Using PHP 5.3 Simple problem?

<?php


$xml = new DOMDocument;
$xml->load('myxml.xml');

$xsl = new DOMDocument;
$xsl->load('myxsl.xsl');

$proc = new XSLTProcessor();
$proc->importStylesheet($xsl);
$newXml = $proc->transformToXML($xml);
$newXML->formatOutput=true;
echo $newXml;

$newXML->save("newfile.xml")or die("Error");
?>

transformToXML() returns a string . You can of course use file_put_contents() to store that string in a file, you can however do it directly with:

 $proc->transformToURI($xml,'file://'.getcwd().'/newfile.xml');

.. or any other directory then the current working dir ( = getcwd() ).

If you want to set some properties / do some tinkering before saving, you might want to:

$newDOM = $proc->transformToDoc($xml);
$newDOM->formatOutput = true;
$newDOM->save("newfile.xml")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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