tags:

views:

48

answers:

4

I was testing this, since I understood using XHTML let me use any valid XML for empty <div> elements:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Test</title>
</head>

<body>
<div style="border:solid 10px black; width:100px; height:100px"></div>
<div style="border:solid 10px red; width:100px; height:100px"></div>
<div style="border:solid 10px blue; width:100px; height:100px"></div>

<div style="border:solid 10px black; width:100px; height:100px" />
<div style="border:solid 10px red; width:100px; height:100px" />
<div style="border:solid 10px blue; width:100px; height:100px" />
</body>
</html>

It doesn't work in any browser I try... this is how FireBug tells me it understands the document:

<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
<title>Test</title>
</head>
<body>
<div style="border: 10px solid black; width: 100px; height: 100px;"/>
<div style="border: 10px solid red; width: 100px; height: 100px;"/>
<div style="border: 10px solid blue; width: 100px; height: 100px;"/>
<div style="border: 10px solid black; width: 100px; height: 100px;">
 <div style="border: 10px solid red; width: 100px; height: 100px;">
  <div style="border: 10px solid blue; width: 100px; height: 100px;"/>
 </div>
</div>
</body>
</html>

I'm a bit confused what the point is of using XHTML if I have to do this, I might as well just use HTML?

Note, that setting the content type to content="application/xhtml+xml" makes no difference in FF3 at least.

+1  A: 

I would take a look at the following question.

What are all the valid self-closing tags in XHTML (as implemented by the major browsers)?

Only some tags are supported as being self closing.

Tracker1
A: 

To answer the question, the XHTML specification only allows ten different tags to have empty elements. As far as I know, the reason for this is to mostly be HTML compatible.

For the sake of documentation, the valid tags are (from here):

  • <base />
  • <meta />
  • <link />
  • <hr />
  • <br />
  • <img />
  • <area />
  • <input />
  • <col />

But your code isn't actually be interpreted as XML anyway, you have a Content-Type: text/html. That tells the browser to interpret your code as normal HTML. To tell the browser to interpret your XHTML as XML you must send the content as application/xhtml+xml instead. There are other problems with this, though.

Brian McKenna
The XML and XHTML specifications allow using the self-closing syntax for any element, but those only apply when you send the document with the correct Content-Type, as you said.
Ms2ger
+2  A: 

what the point is of using XHTML if I have to do this, I might as well just use HTML?

You are using HTML, assuming you're serving your pages as Content-Type: text/html, which (sadly) you have to be at the moment.

If you want XML's markup simplicity advantages, you'd have to serve the page as an XML media type, such as application/xhtml+xml. Unfortunately this will break older browsers and IE. Some people recommend serving XHTML with a variable content type dependent on the browser's Accept request header, but I wouldn't as it means you have two sets of very slightly different markup, styling and scripting rules to adhere to, and it makes cacheing less effective. (If you set Vary: Accept like you should in such a response, it breaks cacheing in IE, and if you don't browsers can get the cached wrong type.)

It is valid to use self-closing form for any element in ‘real’ XHTML, but at the moment you aren't using ‘real’ XHTML, you're using HTML-compatible XHTML as defined by Appendix C of the XHTML standard. HTML-compatible XHTML doesn't really bring any advantages at the browser side, but it does mean you can author your content in XML-aware tools that can easily do things like well-formedness checks, and transform or template the files.

HTML-compatible XHTML requires you to use self-closing tags if (and only if) the element in question has a content model of EMPTY (ie. it can never contain any other elements or text content). The elements in the XHTML 1.0 Strict DTD (and hence also the HTML 4.01 Strict DTD) that are defined EMPTY are:

base
meta
link
hr
br
param
img
area
input
col

The Transitional and Frameset DTDs add:

basefont
isindex
frame
bobince
A: 

This is true. DOCTYPE does not control how document is parsed in browsers. Only MIME type sent in HTTP Content-Type header does.

Detailed explanation of MIME type pitfall.

This is by design:

The HTML WG has discussed this issue: the intention was to allow old (HTML-only) browsers to accept XHTML 1.0 documents by following the guidelines, and serving them as text/html. Therefore, documents served as text/html should be treated as HTML and not as XHTML. There should be no sniffing of text/html documents to see if they are really XHTML.

porneL