Validating Python - need doctype HTML strict

Discussion in 'Python' started by PapaRandy, Jul 10, 2006.

  1. PapaRandy

    PapaRandy Guest

    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!
     
    PapaRandy, Jul 10, 2006
    #1
    1. Advertising

  2. PapaRandy

    Ben Sizer Guest

    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
     
    Ben Sizer, Jul 10, 2006
    #2
    1. Advertising

  3. PapaRandy

    Circa Guest

    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
     
    Circa, Jul 11, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shadow Lynx
    Replies:
    1
    Views:
    553
    Shadow Lynx
    Mar 11, 2006
  2. TheKeith
    Replies:
    9
    Views:
    1,161
    Leif K-Brooks
    Oct 22, 2003
  3. tshad
    Replies:
    14
    Views:
    888
  4. John
    Replies:
    0
    Views:
    423
  5. Larry Lindstrom
    Replies:
    19
    Views:
    1,305
    Jonathan N. Little
    Jun 12, 2012
Loading...

Share This Page