Re: Encode Ampersand for XML

Clemens Klein-Robbenhaar <crobbenhaar-S0/[email protected]>
Newsgroups gmane.comp.web.zope.silva.devel
Message-ID <[email protected]>
Hi Sam,

>  [...] what is the best way in Python (and Zope) to 
> encode the XML special characters ("&", "<", ">") for some XML output. 
> Is there some standard method?
> 

ZPT defines its own helper method; if You are "inside" Zope, You might try:

from TALDefs import attrEscape

(escapes any '"', '<', '>' and '&'; the latter "except when '&' already
begins a syntactically valid entity", whatever that means en detail ...

> Excuse me for posting this question here. I thought there *must* be some 
> standard method in the python library but could not find one :-(

The standard way seems to be "from cgi import escape", which maybe does
not exactly what You want (HTML escape, not XML).
Thats all the little I know, so at least You are not alone ;-)

Cheers,
Clemens
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.