首页 > 解决方案 > 有没有办法将 CSS 链接放入 php 动态生成的 XML 文件中?

问题描述

我已经成功地在 php 中使用 dom 动态生成了一个 xml 文件,但我不确定如何将 CSS 链接放入其中。

我需要 php 代码将 CSS 与自动生成的 XML 文件链接起来。

标签: phpxml

解决方案


要链接 XML 中的 CSS,您可以使用xml-stylesheet处理指令。使用 DOMDocument 上的相应方法创建它,然后像任何其他节点一样附加/插入它。

$document = new DOMDocument();
$document->appendChild(
  $document->createProcessingInstruction(
    'xml-stylesheet', 'type="text/css" href="styles.css"'
  )
);
$document->appendChild(
  $document->createElement('example')
);
echo $document->saveXML();

输出:

<?xml version="1.0"?> 
<?xml-stylesheet type="text/css" href="styles.css"?> 
<example/>

推荐阅读