How can I embed a XML document inside a XHTML document?

与世无争的帅哥 提交于 2020-01-01 15:30:45

问题


For example, how can I embed

<?xml version="1.0" encoding="utf-8"?>
<root>
    <node><![CDATA[Text]]></node>
</root>

in my XHTML?

I know I can put it in a CDATA section...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>My title</title>
</head>
<body>
<p>I am a XHTML paragraph!</p>
<div>
    <![CDATA[
    <?xml version="1.0" encoding="utf-8"?>
    <root>
        <node>Text</node>
    </root>
    ]]>
</div>
</body>
</html>

... but then I have to remove XML file's CDATA sections and the XML is parsed as text instead of XML.

Then, I get this...

... but I want something like this:


回答1:


Please be aware that CDATA is just a way to escape characters, not a method of embedding. Now on to your question:

As far as I know, the only way to embed an XML file would be using the object tag or an iframe:

<!-- Keep in mind that iframe is invalid in XHTML 1.0 and 1.1.-->
<iframe src="data:application/xml,&lt;root&gt;&lt;node&gt;Text&lt;/node&gt;&lt;/root&gt;"/>
<!-- When using `data:mime/type,[data]' the type attribute is overriden -->
<object data="data:application/xml,&lt;root&gt;&lt;node&gt;Text&lt;/node&gt;&lt;/root&gt;"/>

The issue here is that some browsers will just display the contents of the XML as inline text, while others will display with an XML viewer window, which is likely what you want.

Here's a chart:

   layout engine (browser)    |   <object>    |   <iframe>
 ----------------------------------------------------------
  Gecko (Firefox)             |  inline text  |  XML viewer
  Webkit (Safari / new Opera) |  inline text  |  inline text
  Blink (Chrome/Chromium)     |  inline text  |  inline text
  Presto (old Opera)          |  XML Viewer   |  XML viewer
  Trident (IE5-IE8)           |  inline text  |  XML Viewer

Since presto is being replaced, you're left with iframe+firefox as your only workable combination.

If someone can test with IE, please edit-in a your results into the table.



来源:https://stackoverflow.com/questions/15463227/how-can-i-embed-a-xml-document-inside-a-xhtml-document

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!