简体   繁体   English

使用 SimpleXML 生成带有样式表的 xml 文件

[英]Generate xml file with stylesheet using SimpleXML

I have a question about how to add a stylesheet to a SimpleXML generated XML file.我有一个关于如何将样式表添加到 SimpleXML 生成的 XML 文件的问题。 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 文件顶部的行。

<?xml-stylesheet type="text/xsl" href="order.xsl" ?>

您可以尝试在根节点之前添加它

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

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) SimpleXML 插入处理指令(样式表)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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