dougu1952 2010-06-18 08:53
浏览 39
已采纳

PHP DOmDocument,XML和CSS - 无法使其工作

I am generating an XML stream with a DomDocument. I need the pure XML stream, so I don't want to change the sourcecode with an xsl Transformation. Nevertheless I want people to be able to 'read' the XML in their browser without right click->View Source.

So what I do is I create a DomDocument and add a processing instruction:

$css = $xml->createProcessingInstruction("xml-stylesheet", "type=\"text/css\" href=\"xml.css\"");
$xml->appendChild($css);

Then I add all my items. The resulting XML looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="xml.css"?>
<OBJECT>
  <OBJECTNAME>Mi Casa</OBJECTNAME>
  <OBJECTID>1</OBJECTID>
  <DATES>
    <DATE>
      <ID>5</ID>
      <STAYFROM>05.08.2010</STAYFROM>
      <STAYTO>15.08.2010</STAYTO>
    </DATE>
  </DATES>
</OBJECT>

here is the link to the original xml:

http://www.todasmisreservas.com/service/rss.php?obj=1type=res&status=confirmed&xsl=1

and this is my css:

OBJECT {
background-color: #555555;
width: 100%;
}

OBJECTNAME {
display: block;
margin-bottom: 30pt;
margin-left: 0;
}

OBJECTID {
color: #FF0000;
font-size: 20pt;
}

DATES {
color: #0000FF;
font-size: 20pt;
}

STAYFROM, STAYTO, ID {
display: block;
color: #aa0000;
margin-left: 20pt;
}

I can't get it to work that the cssfile affects the xml in any way. What am I doing wrong?

Thanks

Maenny

  • 写回答

1条回答 默认 最新

  • dpg76975 2010-06-18 15:37
    关注

    Ok for anybody with the same problem, I finally found a solution:

    When streaming a XML-stream from php make sure not only to have a valid xml file, but also to send a HTTPResponse-Header like:

    header("content-type: text/xml; charset=utf-8");
    

    Then it works.

    Greetz

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题