我想整合一个XSL
在文件XML
字符串由红粉我php
CURL
命令。 我tryed这
$output = XML gived me by curl option;
$hotel = simplexml_load_string($output);
$hotel->addAttribute('?xml-stylesheet type=”text/xsl” href=”css/stile.xsl”?');
echo $hotel->asXML();
这样做,当我看到浏览器的XML,我收到该文件,而不样式表。 哪里是我的错误?
一个SimpleXMLElement不允许您在默认情况下创建并添加一个处理指令 (PI)的节点。 但是姐姐库DOM文档允许这样做。 您可以通过扩展的SimpleXMLElement娶两个,并创建一个函数来提供该功能:
class MySimpleXMLElement extends SimpleXMLElement
{
public function addProcessingInstruction($target, $data = NULL) {
$node = dom_import_simplexml($this);
$pi = $node->ownerDocument->createProcessingInstruction($target, $data);
$result = $node->appendChild($pi);
return $this;
}
}
这就很容易使用:
$output = '<hotel/>';
$hotel = simplexml_load_string($output, 'MySimpleXMLElement');
$hotel->addProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="style.xsl"');
$hotel->asXML('php://output');
示例性输出(美化):
<?xml version="1.0"?>
<hotel>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
</hotel>
另一种方法是插入一个XML块到一个simplexml的元件: “PHP SimpleXML的:在特定位置插入节点”或“插入XML成一个SimpleXMLElement” 。