Related Entries

India PyCon 2009
Quick wallpaper changer
Load testing with Grinder
Opera RSS to OPML
Soup is beautiful

« Freemind for presentations?
» Load testing with Grinder

Adding namespace to XML

Regex on XML to add a namespace

InfoPath expects a namespace to be defined in the XML. In case you’ve done custom web services that don’t have the heavy payload of SOAP, you might’ve skipped on namespace too. I ran into this issue recently - it is easy enough to front end your web service with a small regex script that attaches a namespace. Here is a small python regex snippet that adds a namespace named my: to an XML.

renode = re.compile("<([A-Za-z0-9_-]+)", re.DOTALL|re.MULTILINE)
reendnode = re.compile("</([A-Za-z0-9_-]+)", re.DOTALL|re.MULTILINE)
reattr = re.compile("\s*([A-Za-z0-9_-]+)\s*=", re.DOTALL|re.MULTILINE)
x = renode.sub(r'<my:\1', x)
x = reendnode.sub(r'</my:\1', x)
x = reattr.sub(r' my:\1=', x)
print x