0

I was trying to make an HTML page and I used:

<img src="something.png"></img>

and

<img src="something.png"/>

Which are both valid XML and and it didn't display. But if I left off the trailing slash or left off the closing tag it worked.

<img src="something.png">

Which is not valid XML. I've found numerous examples of these tags including, "BR" and a few others (possibly UL or OL) that are invalid XML or strange behavior (layout?) when closing them.

I want to make my HTML markup valid XML but have it display in older browsers. I've looked at and heard of XHTML but heard that it is deprecated but I'm not sure. I've tried it and in fact it rendered differently than my HTML page.

My question is, is it possible for me to make my HTML page markup also valid XML and still work in older browsers (not super old but old).

UPDATE:
I've tested the above tags and I have not been able to reproduce the issues (might have been doctype or older or mobile browser). As I was testing I came across another example though. I was using valid XML singletons in many places but they were not loading. I found this case mentioned on Wikipedia:

<script type="text/javascript" src="/script/site.js"/>

Note: The format <script …></script>, rather than the more concise <script … />, is required for HTML compatibility when served as MIME type text/html (from wikipedia)

Background Info:
The reason for this is because I have XML validators at my disposal and it has been great for catching typos and errors. I would much rather write well formed HTML for this reason alone.

The other reason for this is that I'm working on generating some HTML and internally it would be much easier to work with an E4X XML object than writing strings because XML objects have a toXMLString() method which writes the formatted XML out for me but it will make singleton nodes <node /> which, as mentioned above, do not work with some tags (such as the script tag).

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
  • 2
    The `` is right. How come it doesn't display? `:O` – Praveen Kumar Purushothaman Sep 23 '15 at 08:06
  • I might have had the wrong doctype but I'm not sure. I'll try to come up with an example where it is not working – 1.21 gigawatts Sep 23 '15 at 08:09
  • 4
    The doctype will be important here. However, knowing your motivation is also important: why do you want an HTML page to be valid XML? Part of the reason XHTML is deprecated is because while generally a tempting idea, it wasn't entirely practical in many circumstances, partly because web pages are usually part-generated by humans, and partly due to the way browsers handled it. You may also want to look at [this question](http://programmers.stackexchange.com/questions/149839/is-xhtml5-dead-or-is-it-just-an-synonym-of-html5) for a bit of background on mixing XML and modern HTML. – Matt Gibson Sep 23 '15 at 08:13
  • "I've looked at and heard of XHTML but heard that it is deprecated but I'm not sure." — The HTML 5 specification includes an XML serialisation, however pointless that may be. – Quentin Sep 23 '15 at 08:28
  • http://www.w3.org/TR/html5/the-xhtml-syntax.html#the-xhtml-syntax – Quentin Sep 23 '15 at 08:30
  • 1
    `` is fine in XHTML and error corrected to `` by every browser you might care about in HTML. `` is fine in XHTML and in HTML 5 and (incorrectly as it happens) treated as `` by every browser you care about in older versions of HTML. Whatever the problem was, you appear to have misdiagnosed it and there isn't enough information in the question to allow us to figure out what is actually wrong. – Quentin Sep 23 '15 at 08:49
  • @Quentin I was asking, if XHTML is deprecated??? – Praveen Kumar Purushothaman Sep 23 '15 at 08:50
  • 1
    @PraveenKumar I think the best answer you can fit in a comment is "It's complicated, but basically, yes." – Matt Gibson Sep 23 '15 at 08:59
  • @PraveenKumar — HTML 5 doesn't use the term deprecated. It says things are "obsolete". It does not say that about XHTML. So from a technical point of view, no it is not. From a practical point of view, it is more trouble than it is worth and has been since it first came out. It took me about three years to figure that out, so I've been happily free for XHTML for about a decade. – Quentin Sep 23 '15 at 09:13
  • @Quentin AFAIK, XHTML is awesome. I stick with it. LoL. – Praveen Kumar Purushothaman Sep 23 '15 at 09:13
  • XHTML has the benefit that you can parse it with XML tools and the disadvantage that you can't parse it with a regular HTML parser unless you generate to be HTML compatible, which most XML tools will not do by default, which makes using XML tools problematic. So … far more trouble than it is worth. – Quentin Sep 23 '15 at 09:15
  • I've updated the post. I could not reproduce the problems I mentioned with the `img` tag but found out a use case with the script tag. When serving a page as text/html the valid XML string `` is not valid. You must use ``. – 1.21 gigawatts Sep 23 '15 at 09:34
  • @1.21gigawatts — Well yes. That's one of the HTML compatibility issues I mentioned in my last comment. You don't seem to have a question any more though. – Quentin Sep 23 '15 at 09:35
  • Would it be a bad idea to write all of my HTML pages as valid XML so I can check formatting but set the doctype as HTML (HTML5)? I would have to correct for the script tag singleton but I can run an regexp to find and replace those. – 1.21 gigawatts Sep 23 '15 at 09:42
  • @1.21gigawatts Yup, and there will definitely be an existing question somewhere for that; it was a common issue when people were generating XHTML from tools. I seem to remember you can fool most XML generators into spitting out a separate closing tag. But as Quentin says, this is one of many, many reasons people generally just gave up on XHTML. You might be better off stepping back from your problem -- it may be that there's a tool you can use to generate HTML rather than XML that would solve your problem, whatever it is... (If you just want to "check formatting", then use an HTML validator!) – Matt Gibson Sep 23 '15 at 09:42
  • @Quentin I think my question has been answered. Write in XML if I want, use HTML validator, which some are built into the browsers and I think I can use them, or use XML validator, which I have access to. And if I use E4X XML objects to generate HTML, then catch and patch those singleton cases and hope there aren't too many (script is one). The doctype might have had a lot to do with it but I tried a bunch just now and don't see any issues so I might be ok. The answer is I'm not going to use XHTML just HTML (another reason it looks like is because XHTML removed features). – 1.21 gigawatts Sep 23 '15 at 09:48
  • 1
    HTML 5 defines over 100 elements. 15 of them are allowed to use the empty element syntax. – Quentin Sep 23 '15 at 09:52
  • Yup. And I just read, http://stackoverflow.com/a/3558200/441016 and the compatibility guidelines, http://www.w3.org/TR/xhtml-media-types/#C_2. I need to rethink this. – 1.21 gigawatts Sep 23 '15 at 10:15

0 Answers0