XML/HTML Encoding problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dale Strickland-Clark

    #1

    XML/HTML Encoding problem

    A colleague has asked me this and I don't know the answer. Can anyone here
    help with this? Thanks in advance.

    Here is his email:

    I am trying to parse an HTML document using the xml.dom.minidom parser and
    then outputting a valid HTML document, all using the ISO-8859-1 charset.
    For example:

    My input:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <html>
    <head>
    <title></title>
    <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type" />
    </head>
    <body>

    </body>
    </html>

    Desired output:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <html>
    <head>
    <title></title>
    <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type" />
    </head>
    <body>

    </body>
    </html>

    Note that it doesn't matter if the '<?xml version="1.0"
    encoding="ISO-8859-1"?>' header gets stripped.  What does matter is that the
    input document has the 'ISO-8859-1' charset and is an ANSI encoded file.

    The problem I get is that when I run, for example:

    from xml.dom.minidom import parseString
    output = parseString(str HTML).toxml()

    The output is:

    <?xml version="1.0" encoding="iso-8859-1"?>
    <html>
    <head>
    <title/>
    <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type"/>
    </head>
    <body>
    €
    </body>
    </html>

    So it encodes the entity reference to € (Euro sign).  I need it to remain as
    € so that the resulting HTML can render properly in a browser.  Is
    there a way to make the parser not convert the entity references?  Or is
    there a convenient post processing function that will do the conversion?

    --
    Dale Strickland-Clark
    Riverhall Systems www.riverhall.co.uk

  • Sybren Stuvel

    #2
    Re: XML/HTML Encoding problem

    Dale Strickland-Clark enlightened us with:[color=blue]
    > So it encodes the entity reference to € (Euro sign).  I need it to
    > remain as € so that the resulting HTML can render properly in
    > a browser.[/color]

    If you want proper display, why not use UTF-8?

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa

    Comment

    • Duncan Booth

      #3
      Re: XML/HTML Encoding problem

      Dale Strickland-Clark wrote:
      [color=blue]
      > from xml.dom.minidom import parseString
      > output = parseString(str HTML).toxml()
      >
      > The output is:
      >
      ><?xml version="1.0" encoding="iso-8859-1"?>
      ><html>
      ><head>
      ><title/>
      ><meta content="text/html; charset=iso-8859-1"
      >http-equiv="Content-Type"/> </head>
      ><body>
      > €
      ></body>
      ></html>
      >
      > So it encodes the entity reference to € (Euro sign).  I need it to
      > remain as € so that the resulting HTML can render properly in a
      > browser.  Is there a way to make the parser not convert the entity
      > references?  Or is there a convenient post processing function that
      > will do the conversion?[/color]

      First up, when I repeat what you did I don't get the same output. toxml()
      without an encoding argument produces a unicode string, and no encoding
      attribute in the <?xml ...?>

      toxml() only takes a single encoding argument, so unfortunately there isn't
      any way to tell it what to do for unicode characters which are not
      supported in the encoding you are using. However, if you then encode the
      unicode output to ascii with entity escapes, I think you should be alright
      (unless I've missed something):
      [color=blue][color=green][color=darkred]
      >>> from xml.dom.minidom import parseString
      >>> strHTML = '''<?xml version="1.0" encoding="ISO-8859-1"?>[/color][/color][/color]
      <html>
      <head>
      <title></title>
      <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type" />
      </head>
      <body>

      </body>
      </html>'''[color=blue][color=green][color=darkred]
      >>> print parseString(str HTML).toxml().e ncode('ascii', 'xmlcharrefrepl ace')[/color][/color][/color]
      <?xml version="1.0" ?>
      <html>
      <head>
      <title/>
      <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type"/>
      </head>
      <body>

      </body>
      </html>[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      You lose the encoding at the top of the output, but since the output is
      entirely ascii I don't think that matters.

      Comment

      • Dale Strickland-Clark

        #4
        Re: XML/HTML Encoding problem

        Thanks, Duncan. That did the trick.

        If you're EuroPythoning, I'll buy you a drink.

        Cheers.


        Duncan Booth wrote:
        [color=blue]
        > First up, when I repeat what you did I don't get the same output. toxml()
        > without an encoding argument produces a unicode string, and no encoding
        > attribute in the <?xml ...?>
        >
        > toxml() only takes a single encoding argument, so unfortunately there
        > isn't any way to tell it what to do for unicode characters which are not
        > supported in the encoding you are using. However, if you then encode the
        > unicode output to ascii with entity escapes, I think you should be alright
        > (unless I've missed something):
        > You lose the encoding at the top of the output, but since the output is
        > entirely ascii I don't think that matters.[/color]

        --
        Dale Strickland-Clark
        Riverhall Systems www.riverhall.co.uk

        Comment

        Working...