[ZPT] XML declaration + DOCTYPE script for ZPT

Tonico Strasser contact_tonico at yahoo.de
Mon Jul 5 07:09:07 EDT 2004


Hello,

I use this (attached) script to serve ZPTs as XHTML.

This is very useful, because browsers can throw XML parsing errors if 
they get not well-formed documents if served as 'application/xhtml+xml'.

Note: The ZPT Content-Type should be 'text/html' for backward compatibility.

Feedback welcome.

Example usage:

'main_template'

   <metal:template define-macro="master"
   ><tal:prolog replace="structure here/prolog"/>
   <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
     [...]
   </html></metal:template>

'foo_template'

   <metal:template use-macro="here/main_template/macros/master"/>

References

Serving XHTML 1.0
   <http://www.w3.org/International/articles/serving-xhtml/>

List of valid DTDs you can use in your document.
   <http://www.w3.org/QA/2002/04/valid-dtd-list.html>

XHTML Media Types
   <http://www.w3.org/TR/2002/NOTE-xhtml-media-types-20020801/>

Activating the Right Layout Mode Using the Doctype Declaration
   <http://www.hut.fi/u/hsivonen/doctype.html>

DOCTYPEs in ZPT
   <http://www.zopelabs.com/cookbook/1024822369>

Using a DOCTYPE with ZPT
   <http://www.zopelabs.com/cookbook/1018934697>

The Road to XHTML 2.0: MIME Types
   <http://www.xml.com/pub/a/2003/03/19/dive-into-xml.html>

Serving up XHTML with the correct MIME type
   <http://keystonewebsites.com/articles/mime_type.php>

Sending XHTML as text/html Considered Harmful
   <http://www.hixie.ch/advocacy/xhtml>

Tonico
-------------- next part --------------
## Script (Python) "prolog"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=encoding="ISO-8859-1"
##title=
##
request = container.REQUEST
response =  request.RESPONSE
xml_prolog = '<?xml version="1.0" encoding="%s"?>' % encoding
doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
media_type = 'application/xhtml+xml'
accept_xhtml = request['HTTP_ACCEPT'].find(media_type) > -1 
prolog = xml_prolog + '\n' + doctype

if not accept_xhtml:
    media_type = 'text/html'
    comment = """\
<!-- Note: Your Browser does not accept 'application/xhtml+xml'.
     This page is served as 'text/html'. -->"""
    prolog = doctype + '\n' + comment

response.setHeader('Variy', 'Accept')
response.setHeader('Content-Type', '%s;charset=%s' % (media_type, encoding))
return prolog


More information about the ZPT mailing list