0

I have a question about how to add a stylesheet to a SimpleXML generated XML file. This is my code, but I don't know how to add a stylesheet for this.

$xml = new SimpleXMLElement('<Cart/>');

$Order = $xml->addChild('Person');
$Order->addChild('Name', $_GET['name']);
$Order->addChild('Last-name', $_GET['lname']);
$Order->addChild('E-mail', $_GET['email']);
$Order->addChild('Phone', $_GET['phone']);
$Order->addChild('Date', date('Y-m-d'));
$Order->addChild('Adress', isset($_GET['adress'])&&$_GET['adress'] != NULL?$_GET['adress']:'Not set');
$Products = $xml->addChild('Products');
foreach ($cart as $product_id) {
    foreach($productlist as $list){
        if($list['id'] == $product_id){
            $Cart = $Products->addChild('Product');
            $Cart->addChild('ID', $list['id']);
            $Cart->addChild('Brand', $list['brand']);
            $Cart->addChild('Model', $list['model']);
            $Cart->addChild('Price', $list['price']);
        }
    }
}

Header('Content-type: text/xml');
date_default_timezone_set("Europe/Helsinki");
$xml->asXML('orders/' .date('Y-m-d(H-i-s)'). '.xml');

And this is the line that I want to add to the top of my generated XML file.

<?xml-stylesheet type="text/xsl" href="order.xsl" ?>
IMSoP
  • 89,526
  • 13
  • 117
  • 169
Lucian
  • 23
  • 4
  • Possible duplicate: [SimpleXML insert Processing Instruction (Stylesheet)](https://stackoverflow.com/questions/16567055/simplexml-insert-processing-instruction-stylesheet) – IMSoP Jul 06 '20 at 08:43

2 Answers2

1

You can try just adding it before the root node

 $xml = new SimpleXMLElement('<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="order.xsl"?><Cart/>');
Daniel ML
  • 11
  • 4
0

you need to write

$xml->addProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="order.xsl"'); 

how to add function you can view here

SimpleXML insert Processing Instruction (Stylesheet)

Community
  • 1
  • 1
  • This gives me a fatal error: Fatal error: Call to undefined method SimpleXMLElement::addProcessingInstruction() – Lucian Sep 12 '14 at 09:03
  • It's a custom method which needs to be defined - see the linked question. – acme Mar 03 '15 at 15:43