0

This was the closest question to mine that I could find: Closing empty elements and other xhtml standards within HTML5 and it leaves me disturbed, alarmed!

I have for decades used XML / XSLT to generate web pages. At some point I just used xsl:output type xml, not even html or xhtml. I thought: HTML is XML (at least somehow). But now, as I'm back actually typing HTML I find myself teleported to the early 1990s or something, where I have to write

<script src="test.js"></script>

instead of just

<script src="test.js"/>

and

<input>
<br>
<img>

don't need to be closed at all.

I sometimes do

<div/>
<p/>

and now I notice that the /> is entirely ignored and the p sucked into the contents of the div, leading to all kinds of confusion.

Isn't there some mode I can set on top, or by content-type or whatever it is where I can tell the browser to parse my document correctly as XML? I tried to begin the file with:

<?xml version="1.0"?>

but to no avail.

It feels like I'm the only one in the world who has completely missed the boat after decades of building web applications.

Gunther Schadow
  • 1,490
  • 13
  • 22
  • 1
    Hello Gunther. I find this area a bit baffling too. But what is the media type in the HTTP headers, does this affect things? – Michael Kay Dec 10 '22 at 09:17
  • 1
    If you want to author XHTML according to XML syntax rules then make sure you serve your documents not as `text/html` but rather as `application/xhtml+xml` or as `application/xml`. So yes, it is the content type, set as as HTTP Content-Type header, that matters how the browser parses your document. If you load from the local file system then save as `.xml` or as `.xhtml` (for the latter I don't know how good it is supported on various platforms/OS). – Martin Honnen Dec 10 '22 at 11:12
  • 1
    Also note, once you go the XML route, for browser to recognize e.g. `p` or `div` or any other elements as XHTML elements, make sure you declare the XHTML namespace `..` on the root element. – Martin Honnen Dec 10 '22 at 11:14
  • @MichaelKay, good that you would reply here, because I noticed when using Saxon-js that I need to be strictly XHTML and use the right namespace. In the other answer I see that the media type should be application/xhtml+xml, I wonder why not text/xml. I haven't tried it yet, probably different from browser to browser. – Gunther Schadow Dec 11 '22 at 04:17
  • 1
    `application/xhtml+xml` works fine cross-browser. The last browser not to support it was Internet Explorer 8, which is now long gone. – Alohci Dec 12 '22 at 02:57
  • @MartinHonnen but does text/xml not work? Only application/xhtml+xml or application/xml? – Gunther Schadow Dec 20 '22 at 04:27
  • @GuntherSchadow, when I learned how to properly use the MIME type to ensure XML parsing for X(HT)ML documents (which was probably a decade and a half ago) someone seeming rather knowledgeable recommended not to go with text/xml due to various shortcomings/restrictions. I can't name them now, I don't know whether, like many things in the browser world, they have changed for good or worse, but I tend to use and recommend `application/xml` and `application/xhtml+xml` and it has worked out nice for me. If `text/xml` works for you, use it. – Martin Honnen Dec 20 '22 at 08:47

0 Answers0