Serving XHTML and self-closing tags

帅比萌擦擦* 提交于 2021-01-29 16:18:52

问题


I am trying to follow the xhtml 1.0 strict standard as I am creating my website. Right now, validator.w3.org validates my document as valid, following the XHTML 1.0 Strict standard. Here is a code-example:

<div style="color:#f00"><div style="color:#00f" />Text should be red and not blue</div>

Unfortunately, Firefox, Chrome and Internet Explorer parses the document incorrectly: They all seem to ignore the closing statement of my self-closing tags (mostly <div />, <li /> and some other tags), thus rendering the site incorrectly, with the text being blue. if i replace <div /> tags with <div></div>, it all looks fine. I read about it and according to xml document, including xhtml, can be self-closed

Here is the important header information that comes with the document:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
        <head>
            <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />

Apache2 itself still sends Content-Type text/html, as I haven't figured out yet how to overwrite the .php mime type, though the info in <head> should overrule that either way.

How can I use self-closing tags such as <div /> with them being parsed correctly by common browsers?


回答1:


XHTML is HTML using XML and HTML does not have self-closing tags like you show. This works in XHTML but not HTML and, so far, you are not serving as application/xml+xhtml.

The content-type meta tag does not affect how the server serves the page and is for offline use only. So you must either set it in the server or with PHP, in your case.




回答2:


Sending this with php:

<?php
header('Content-Type: application/xhtml+xml;');
?>

That's how you override standard headers using PHP. You have to be careful, because header() only works if no output has been sent yet. This means you must not place anything before the <?php part, header will not work as your server will already have sent both the headers and whatever content there was.



来源:https://stackoverflow.com/questions/8645642/serving-xhtml-and-self-closing-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!