1

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.

1 Answer 1

1

But I'm not reading in a XML file because I'm generating strings for the data and this generates '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:

 $data = $xslt->transformToXML($xsl);

In the transformation you will have a single template (matching /) and inside this template you'll generate the wanted XML document - result.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.