By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,544 Members | 1,470 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,544 IT Pros & Developers. It's quick & easy.

Validating Python - need doctype HTML strict

P: n/a

Hello,

I am trying to validate the following .py webpage as HTML (through
W3C).

I put:

-----------------------------------------------------------------------------

print "Content-type: text/html; charset=utf-8"

import time

print

print """<html><head>

<title>Current Time</title>

</head<body>

<h1>Current Time</h1>"""

print "<p>Right now, it is "

print "<strong>", time.asctime(), "</strong></p>"

print "</body></html>"

---------------------------------------------------

However, the .py page should also be valid (X)HTML - it needs a
doctype...

When I add the doctype, and other necessities to the .py page

(i.e., <!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">)

I get an invalid script error.

Can anyone help me to get the XHTML doctype to validate and execute on
Python web page?

Thank you!

Jul 9 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
PapaRandy wrote:
When I add the doctype, and other necessities to the .py page

(i.e., <!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">)

I get an invalid script error.
Bear in mind, that this isn't 'a webpage', it's a computer program that
creates a webpage. So you're not 'adding a doctype', you're 'adding
some code to output a doctype', and similarly it's not Python you're
validating, it's the output it creates that you validate. These are
important distinctions.

Anyway, what is the exact line of code you use to 'add the doctype'?
And what is this 'invalid script error'? It's hard to debug your code
when we have to guess what it is! However, because I'm in a good mood,
I'll have a go.

You probably need to escape the double quotes in the doctype because
they unintentionally correspond with the double quotes in your print
statement. The print statement uses double quotes to delimit the
output, and the doctype uses them to delimit the type. Unfortunately
the print statement probably interprets the start of the doctype's type
field as the end of the print statement. Add a backslash before each
double quote within your doctype and see how that goes. Alternatively
you could possibly use single quotes in it instead.

--
Ben Sizer

Jul 10 '06 #2

P: n/a
Thanks Ben,

As you can see it's my first try at Python...or any programming for
that matter.

I solved the problem of validation by adding three """ quotes after the
print command and before the body, as follows:

Thanks for clearing that up!

-----------------------

import time

#print HTTP/HTML header stuff
print """Content-type: text/html; charset=utf-8\n\n

<!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>Current Time</title>

</head<body>
"""

print

#print HTML body using Python-HTML hybrid script
print "<h1>Current Time</h1>"

print "<p>Right now, it is "

print "<strong>", time.asctime(), "</strong></p>"

print """<p>
<a href="http://validator.w3.org/check?uri=referer"><img
src="http://www.w3.org/Icons/valid-xhtml10"
alt="Valid XHTML 1.0 Strict" height="31" width="88" /></a>
</p>
"""

print "</body></html>"
Ben Sizer wrote:
PapaRandy wrote:
When I add the doctype, and other necessities to the .py page

(i.e., <!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">)

I get an invalid script error.

Bear in mind, that this isn't 'a webpage', it's a computer program that
creates a webpage. So you're not 'adding a doctype', you're 'adding
some code to output a doctype', and similarly it's not Python you're
validating, it's the output it creates that you validate. These are
important distinctions.

Anyway, what is the exact line of code you use to 'add the doctype'?
And what is this 'invalid script error'? It's hard to debug your code
when we have to guess what it is! However, because I'm in a good mood,
I'll have a go.

You probably need to escape the double quotes in the doctype because
they unintentionally correspond with the double quotes in your print
statement. The print statement uses double quotes to delimit the
output, and the doctype uses them to delimit the type. Unfortunately
the print statement probably interprets the start of the doctype's type
field as the end of the print statement. Add a backslash before each
double quote within your doctype and see how that goes. Alternatively
you could possibly use single quotes in it instead.

--
Ben Sizer
Jul 11 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.