• Login or register

discuss.effbot.org

  • Popular
  • Recent
  • 2 points by directrix1 1 year ago on Elements and Element Trees (Brief Tut... 0 children

    I wrote a small function that will strip out either a list of namespaces (if you pass in ['http://ns1','http://ns2'] as nss) or all namespaces (if you pass in ['*'] as nss). Enjoy:


    def stripns(x,nss):
    a = {}
    for t in x.attrib:
    a[t] = x.attrib[t]
    for dns in nss:
    ns = '{'+dns+'}'
    lenns = len(ns)
    if ns=='{*}':
    x.tag = x.tag[x.tag.find('}')+1:]
    elif x.tag.find(ns)==0:
    x.tag = x.tag[lenns:]
    for i in x.attrib:
    if ns=='{*}':
    del a[i]
    a[i[i.find('}')+1:]] = x.attrib[i]
    elif i.find(ns)==0:
    del a[i]
    a[i[lenns:]] = x.attrib[i]
    x.attrib = a
    for i in x.findall('*'):
    stripns(i,nss)

    • parent
    • reply
  • Widget
  • Recent Comments
  • Leaders
Powered by