27

How would one go about embedding XML in a HTML page?

I was thinking using CDDATA would be the best approach but I get errors in the HTML document when the page loads.

<script><![CDATA[ ... ]]></script>

I'm needing to embed a XML document for fetching later with JavaScript. I need to do this since when the user opens it, they might not have internet access.

amcdnl
  • 8,470
  • 12
  • 63
  • 99
  • CDATA is meant for embedding character data into XML, not embedding XML into HTML (HTML doesn't have such delimiters). – BoltClock Oct 03 '12 at 15:47
  • As long as the XML doesn't contain `` anywhere, you should be able to just put it inside the script tags (with no CDATA section). Give the script a custom type so it doesn't get interpreted as JavaScript. – Dagg Nabbit Oct 03 '12 at 15:48
  • This might be helpful: http://stackoverflow.com/questions/7816500/embed-xml-in-html-firefox-compatible – MikeB Oct 03 '12 at 15:50
  • @RayToal, not necessary - http://jsfiddle.net/hJuPs/ – Dagg Nabbit Oct 03 '12 at 15:51
  • Ya, when I try to just drop it in the script tags I get: Uncaught SyntaxError: Unexpected token – amcdnl Oct 03 '12 at 15:53
  • @AustinMcDaniel see my answer, it needs a `type` attribute other than "text/javascript" – Dagg Nabbit Oct 03 '12 at 15:54
  • @GGG You are right, thanks. [No escaping is necessary](http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html#restrictions-for-contents-of-script-elements) despite what these horribly, horribly, annoying Eclipse plugins have been telling me. – Ray Toal Oct 03 '12 at 16:04
  • 1
    @RayToal not only is it not necessary, it won't work at all... in XML terms, you'd say `script` expects CDATA content, not PCDATA (not sure what the appropriate terminology is in HTML-speak). – Dagg Nabbit Oct 03 '12 at 16:06
  • +1 Got it. In HTML 4 and earlier, `script` element bodies are CDATA, not PCDATA. In HTML 5 and up, they don't seem to show this distinction anymore, though they do allow `<![CDATA[` in foreign content (e.g. MathML or SVG). Script body content restrictions are now just described in prose. – Ray Toal Oct 03 '12 at 16:20
  • @RayToal: in current HTML, `script` (along with `style`) is [acknowledged to be] a very special beast with syntax quite unlike anything seen in XML or SGML. I mean, the entire content is **allowed to be commented out** while still being used! That's pretty special. – SamB Mar 18 '19 at 20:54

3 Answers3

37

As long as the XML doesn't contain </script> anywhere, you can put it inside the script tags with a custom type attribute (and no CDATA section). Give the script tag an id attribute so you can fetch the content.

<script id="myxml" type="text/xmldata">
    <x>
        <y z="foo">

        </y>
    </x>
</script>​

... 

<script> alert(document.getElementById('myxml').innerHTML);​ </script>

http://jsfiddle.net/hJuPs/

Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
  • Setting type="text/xml" was the trick I was missing! Thanks! – amcdnl Oct 03 '12 at 15:58
  • 1
    @AustinMcDaniel note that it doesn't have to be "text/xml" ... it just has to *not* be "text/javascript" – Dagg Nabbit Oct 03 '12 at 16:00
  • 3
    And also note the type is "text/javascript" by default in HTML5 if omitted. – Kamiel Wanrooij Oct 03 '12 at 16:03
  • @AustinMcDaniel avoid the type `text/xml`! In the [spec](http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html#scriptingLanguages) it lists "text/xml" as one of the MIME types that "must not be interpreted as scripting languages". However it also says that the `script` element is designed to store data blocks. Interesting. – Ray Toal Oct 03 '12 at 16:09
  • 1
    @RayToal ah, that's good to know. I wonder if `text/xml` gets special treatment in some implementations? I changed it to a custom mime type :) edit - but wait, isn't not being interpreted as a scripting language exactly what we want? I think it means `text/xml` is reserved for the sort of thing we're trying to do, no? – Dagg Nabbit Oct 03 '12 at 16:17
0

How about:

<script>
    var xml = '<element> \
                 <childElement attr="value" /> \
               </element>';
</script>

That would enable you to easily embed the XML for later retrieval in javascript.

Kamiel Wanrooij
  • 12,164
  • 6
  • 37
  • 43
0

According to the tutorial here, you can use the 'xml' tag to embed XML data within an HTML document. However, this implicitly displays the XML data in the browser.

http://www.expertrating.com/courseware/XMLCourse/XML-Embedding-HTML-8.asp

Chong Lip Phang
  • 8,755
  • 5
  • 65
  • 100
  • According to the link here, it has been deprecated. However, I have never seen this site before, nor have I had heard about the tag before seeing this stack question. http://www.html-5.com/changes/deprecated/xml-tag.html – jwp Nov 05 '15 at 18:43
  • 1
    XML is still widely used all over the world in nearly 9 out of 10 applications whether some web guy "didn't like it" and "json is better" (the actual real reason it was deprecated). I hope an alternative is provided for embeding XML or that's it's precated. – 1.21 gigawatts Dec 01 '18 at 00:50
  • @1.21gigawatts I doubt if that's the real reason the *element* was dropped. According to archived MDN page [Using XML Data Islands in Mozilla ](https://developer.mozilla.org/en-US/docs/Archive/Misc_top_level/Using_XML_Data_Islands_in_Mozilla), the feature was only ever supported in Internet Explorer, and Internet Explorer 10 *doesn't even support it anymore*. It seems pretty likely it never got used much use compared to the ` – SamB Mar 18 '19 at 21:13