@@ -277,11 +277,8 @@ def nspath_eval(xpath, namespaces):
277
277
278
278
def cleanup_namespaces (element ):
279
279
""" Remove unused namespaces from an element """
280
- if etree .__name__ == 'lxml.etree' :
281
- etree .cleanup_namespaces (element )
282
- return element
283
- else :
284
- return etree .fromstring (etree .tostring (element ))
280
+ etree .cleanup_namespaces (element )
281
+ return element
285
282
286
283
287
284
def add_namespaces (root , ns_keys ):
@@ -292,35 +289,34 @@ def add_namespaces(root, ns_keys):
292
289
293
290
ns_keys = [(x , namespaces .get_namespace (x )) for x in ns_keys ]
294
291
295
- if etree .__name__ != 'lxml.etree' :
296
- # We can just add more namespaces when not using lxml.
297
- # We can't re-add an existing namespaces. Get a list of current
298
- # namespaces in use
299
- existing_namespaces = set ()
300
- for elem in root .iter ():
301
- if elem .tag [0 ] == "{" :
302
- uri , tag = elem .tag [1 :].split ("}" )
303
- existing_namespaces .add (namespaces .get_namespace_from_url (uri ))
304
- for key , link in ns_keys :
305
- if link is not None and key not in existing_namespaces :
306
- root .set ("xmlns:%s" % key , link )
307
- return root
308
- else :
309
- # lxml does not support setting xmlns attributes
310
- # Update the elements nsmap with new namespaces
311
- new_map = root .nsmap
312
- for key , link in ns_keys :
313
- if link is not None :
314
- new_map [key ] = link
315
- # Recreate the root element with updated nsmap
316
- new_root = etree .Element (root .tag , nsmap = new_map )
317
- # Carry over attributes
318
- for a , v in list (root .items ()):
319
- new_root .set (a , v )
320
- # Carry over children
321
- for child in root :
322
- new_root .append (deepcopy (child ))
323
- return new_root
292
+ # lxml does not support setting xmlns attributes
293
+ # Update the elements nsmap with new namespaces
294
+ new_map = root .nsmap
295
+ for key , link in ns_keys :
296
+ if link is not None :
297
+ new_map [key ] = link
298
+ # Recreate the root element with updated nsmap
299
+ new_root = etree .Element (root .tag , nsmap = new_map )
300
+ # Carry over attributes
301
+ for a , v in list (root .items ()):
302
+ new_root .set (a , v )
303
+ # Carry over children
304
+ for child in root :
305
+ new_root .append (deepcopy (child ))
306
+ return new_root
307
+
308
+ # We can just add more namespaces when not using lxml.
309
+ # We can't re-add an existing namespaces. Get a list of current
310
+ # namespaces in use
311
+ existing_namespaces = set ()
312
+ for elem in root .iter ():
313
+ if elem .tag [0 ] == "{" :
314
+ uri , tag = elem .tag [1 :].split ("}" )
315
+ existing_namespaces .add (namespaces .get_namespace_from_url (uri ))
316
+ for key , link in ns_keys :
317
+ if link is not None and key not in existing_namespaces :
318
+ root .set ("xmlns:%s" % key , link )
319
+ return root
324
320
325
321
326
322
def getXMLInteger (elem , tag ):
@@ -519,21 +515,14 @@ def element_to_string(element, encoding=None, xml_declaration=False):
519
515
if encoding is None :
520
516
encoding = "ISO-8859-1"
521
517
522
- if etree .__name__ == 'lxml.etree' :
523
- if xml_declaration :
524
- if encoding in ['unicode' , 'utf-8' ]:
525
- output = '<?xml version="1.0" encoding="utf-8" standalone="no"?>\n {}' .format (
526
- etree .tostring (element , encoding = 'unicode' ))
527
- else :
528
- output = etree .tostring (element , encoding = encoding , xml_declaration = True )
518
+ if xml_declaration :
519
+ if encoding in ['unicode' , 'utf-8' ]:
520
+ output = '<?xml version="1.0" encoding="utf-8" standalone="no"?>\n {}' .format (
521
+ etree .tostring (element , encoding = 'unicode' ))
529
522
else :
530
- output = etree .tostring (element )
523
+ output = etree .tostring (element , encoding = encoding , xml_declaration = True )
531
524
else :
532
- if xml_declaration :
533
- output = '<?xml version="1.0" encoding="{}" standalone="no"?>\n {}' .format (
534
- encoding , etree .tostring (element , encoding = encoding ))
535
- else :
536
- output = etree .tostring (element )
525
+ output = etree .tostring (element )
537
526
538
527
return output
539
528
@@ -777,21 +766,6 @@ def bind_url(url):
777
766
log .addHandler (NullHandler ())
778
767
779
768
780
- def which_etree ():
781
- """decipher which etree library is being used by OWSLib"""
782
-
783
- which_etree = None
784
-
785
- if 'lxml' in etree .__file__ :
786
- which_etree = 'lxml.etree'
787
- elif 'xml/etree' in etree .__file__ :
788
- which_etree = 'xml.etree'
789
- elif 'elementree' in etree .__file__ :
790
- which_etree = 'elementtree.ElementTree'
791
-
792
- return which_etree
793
-
794
-
795
769
def findall (root , xpath , attribute_name = None , attribute_value = None ):
796
770
"""Find elements recursively from given root element based on
797
771
xpath and possibly given attribute
0 commit comments