xmlentities

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ola Natvig

    #1

    xmlentities

    Hi all

    Does anyone know a good library for transfering non standard characters
    to enity characters in html. I want characters like < and > to be
    transformed to &lt; and &gt;. And the norwegian ø to &oslash;

    ola
    --
    --------------------------------------
    Ola Natvig <ola.natvig@inf osense.no>
    infoSense AS / development
  • Duncan Booth

    #2
    Re: xmlentities

    Ola Natvig wrote:
    [color=blue]
    > Does anyone know a good library for transfering non standard characters
    > to enity characters in html. I want characters like < and > to be
    > transformed to &lt; and &gt;. And the norwegian ø to &oslash;
    >[/color]

    You could use cgi.escape to handle &, <, and > and then use error handling
    on unicode.encode to handle the other characters. That doesn't do quite
    what you ask since your ø will become &#248:
    [color=blue][color=green][color=darkred]
    >>> s = u'<ø>'
    >>> cgi.escape(s).e ncode('ascii', 'xmlcharrefrepl ace')[/color][/color][/color]
    '&lt;&#248;&gt; '


    If you really want named entities, then have a look at
    lib/test/test_codeccallb acks which has a test called
    test_xmlcharnam ereplace that registers another codec error handler
    'test.xmlcharna mereplace'. I think you could probably extract that and use
    it as above.

    Comment

    • Fredrik Lundh

      #3
      Re: xmlentities

      Ola Natvig wrote:
      [color=blue]
      > Does anyone know a good library for transfering non standard characters to enity characters in
      > html. I want characters like < and > to be transformed to &lt; and &gt;. And the norwegian ø to
      > &oslash;[/color]

      the third example on



      includes a function that translates an ISO-8859-1 encoded string to HTML-
      encoded text.

      </F>



      Comment

      Working...