I’ve used the following piece of code to transform XML to another XML file. But I’m trying to use XSLT to create a new XML file from a set of strings.
$xsl = simplexml_load_file('template.xsl');
$xslt = new XSLTProcessor;
$xslt->setParameter('', 'foo', $foo);
$xslt->setParameter('', 'bar', $bar);
$xslt->setParameter('', 'test', $test);
$xslt->importStyleSheet($xsl);
$data = $xslt->transformToXML($xml);
So what I’m doing is passing parameters to template.xsl which creates nodes and the resulting output is passed to $data, which PHP then writes to a file.
But the example code that I have expects a XML file to be read first and saved to a variable, $xml. But I’m not reading in a XML file because I’m generating strings for the data and this generates a warning:
PHP Warning: XSLTProcessor::transformToXml() expects parameter 1 to be object, null given
I realise that I can use PHP to write the XML – http://www.developerfusion.com/code/3944/how-to-create-xml-files/ – But I would like to know how to do this with XSLT.
You can use any XML document to pass as the first parameter of
XSLTProcessor::transformToXml()It can be an artificial document such as
<t/>— loaded from file.In order not to load any additional XML file, one can use the already loaded XSLT file:
In the transformation you will have a single template (matching
/) and inside this template you’ll generate the wanted XML document – result.