How to use result of nsmap in xpath

hello, "nsmap" is an lxml.element function to get namespaces of the element, usually, a None type will be returned, like: {'olac': 'http://www.language-archives.org/OLAC/1.1/', None: 'http://www.imsglobal.org/xsd/imscp_v1p1'} i want use this namespaces in XPath, but i got an error: TypeError: empty namespace prefix is not supported in XPath so, i must delete this NoneType by creating a new dictionay in Python: dic_ns = {} for element in root.nsmap: if element is None: continue dic_ns[element] = root.nsmap[element] QUESTION: is there some other methods to do this? Thank you in advance, Bests, Kun

Hi, Am Dienstag, 30. September 2014, 10:07:47 schrieb Kun JIN:
The for loop is too complicated. ;) There is no need to generate a new dictionary. Just delete the key/value with pop(): dic_ns.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised To remove any key with None in your dictionary, pass None as the first argument. The second None is used to avoid the KeyError exception, in case there is no key with None: dic_ns.pop(None, None) -- Gruß/Regards Thomas Schraitle

Kun JIN schrieb am 30.09.2014 um 10:07:
http://lxml.de/FAQ.html#how-can-i-find-out-which-namespace-prefixes-are-used... And the one that follows. Stefan

Hi, Am Dienstag, 30. September 2014, 10:07:47 schrieb Kun JIN:
The for loop is too complicated. ;) There is no need to generate a new dictionary. Just delete the key/value with pop(): dic_ns.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised To remove any key with None in your dictionary, pass None as the first argument. The second None is used to avoid the KeyError exception, in case there is no key with None: dic_ns.pop(None, None) -- Gruß/Regards Thomas Schraitle

Kun JIN schrieb am 30.09.2014 um 10:07:
http://lxml.de/FAQ.html#how-can-i-find-out-which-namespace-prefixes-are-used... And the one that follows. Stefan
participants (3)
-
Kun JIN
-
Stefan Behnel
-
Thomas Schraitle