Oriol
Oriol

Reputation: 288080

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

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...

enter image description here

... but I want something like this:

enter image description here

Upvotes: 3

Views: 2989

Answers (1)

Hawken
Hawken

Reputation: 2119

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.

Upvotes: 3

Related Questions