[silva-dev] Encode Ampersand for XML

Clemens Klein-Robbenhaar crobbenhaar at web.de
Mon Sep 5 23:44:37 CEST 2005


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



More information about the silva-dev mailing list