[mod_python] CSS not processed in PSP stylesheet&[email protected]

Byron Ellacott bje at apnic.net
Mon Jan 10 18:12:32 EST 2005


pit.grinja at gmx.de wrote:
> Many thanks for your hints. In fact, the problem had nothing to do with an
> incorrect path, and the content type 'text/html' is in principle ok. The
> problem seems to be the combination of the content type AND the xml
> declaration. It turned out that the directly opening the file in mozilla did
> not give the expected results (<b>sometext</b>,<i>someother</i>,<br />...
> parts were ignored if not defined in the css). After lots of
> experimentation, I found that all what is needed is the following tag as a
> child of the head-tag:
> <meta content="application/xhtml+xml"/>.

There are a couple of caveats to beware of here.  First, as you have 
probably surmised, <?xml-stylesheet ...?> is only valid in an XML 
document, which text/html is not.  Even if you send an XHTML document, 
complete with a <?xml ...?> opener, browsers will /not/ treat it as an 
XHTML document.  This is trivial to demonstrate with the following page:

---------------------
<?xml version="1.0"?>
<!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" lang="en">
   <head>
     <title>Test page</title>
   </head>
   <body>
     <p>
       <script type="text/javascript">
           document.write('Hello, World!');
       </script>
     </p>
   </body>
</html>
--------------------

If you serve this file up as text/html, you will get a blank page with 
'Hello, World!' on it.  If you serve this file up as 
application/xhtml+xml, you will get a blank page.  This is because an 
XHTML document is parsed differently; a DOM tree is built before 
anything is interpreted, to ensure the document is valid, and so the 
document.write does not happen until after the parse, when it's too late 
to add anything new to the parse.

Also, Internet Explorer will ask if you wish to save the file to disk, 
since it does not recognise XHTML.

Since you have direct control over the MIME type you're sending, I would 
recommend that you send the correct MIME type directly, rather than 
relying on a <meta ...> element.

Also note that in the example you gave, your DOCTYPE was not entirely 
correct -- the system should not be a relative URL, it should be the 
absolute URL pointing to the w3c's dtd for XHTML.  I use XHTML Strict 
when I produce XHTML.

> Now I have a file that is well-formed, valid and correctly rendered in the
> browser with respect to the standard html tags.

Beware well-formedness and validity with XHTML.  If you do not send the 
correct content-type to the w3c's validator, your page will not be 
validated as XHTML!  Also beware the subtle differences in how CSS 
applies to XHTML documents compared to HTML.  Most notably, XHTML 
elements are case sensitive, so your CSS selectors must be all lower 
case.  Also, CSS attributes you would normally apply to a body element 
must instead be applied to an html element.

Finally, it bears repeating: Internet Explorer, all versions, does not 
recognise XHTML.  As far as I know, it never will.  If you need to 
support IE, you are far better off producing HTML4.01 Strict than XHTML1 
Transitional, since HTML4.01 Strict is understood by all browsers with 
its correct MIME type.

See http://www.hixie.ch/advocacy/xhtml for an explanation of XHTML and 
HTML differences, and why XHTML should never be sent as "text/html".

-- 
bje


More information about the Mod_python mailing list