def fix_ns(xpath, split=False, propagate_ns=True, default_ns=u''):
 fixed = []
 ns_blocks = xpath.split(u'{')
 for ns_block in ns_blocks:
  if (u'}' in ns_block):
   namespace = ns_block.split(u'}')[0]
   elements = ns_block.split(u'}')[1].split(u'/')
  else:
   namespace = default_ns
   elements = ns_block.split(u'/')
  for element in elements:
   if element:
    if (propagate_ns and (element[0] != u'*')):
     tag = (u'{%s}%s' % (namespace, element))
    else:
     tag = element
    fixed.append(tag)
 if split:
  return fixed
 return u'/'.join(fixed)

def fix_ns ( xpath , split = False , propagate_ns = True , default_ns = u'' ) : fixed = [ ] ns_blocks = xpath . split ( u'{' ) for ns_block in ns_blocks : if ( u'}' in ns_block ) : namespace = ns_block . split ( u'}' ) [ 0 ] elements = ns_block . split ( u'}' ) [ 1 ] . split ( u'/' ) else : namespace = default_ns elements = ns_block . split ( u'/' ) for element in elements : if element : if ( propagate_ns and ( element [ 0 ] != u'*' ) ) : tag = ( u'{%s}%s' % ( namespace , element ) ) else : tag = element fixed . append ( tag ) if split : return fixed return u'/' . join ( fixed )
apply the stanzas namespace to elements in an xpath expression .