Python xml.etree.ElementTree 模块,_namespace_map() 实例源码
我们从Python开源项目中,提取了以下15个代码示例,用于说明如何使用xml.etree.ElementTree._namespace_map()。
def register_namespace_prefixes():
from saml2 import md, saml, samlp
try:
from saml2 import xmlenc
from saml2 import xmldsig
except ImportError:
import xmlenc
import xmldsig
prefixes = (('saml', saml.NAMESPACE),
('samlp', samlp.NAMESPACE),
('md', md.NAMESPACE),
('ds', xmldsig.NAMESPACE),
('xenc', xmlenc.NAMESPACE))
if hasattr(ElementTree, 'register_namespace'):
for prefix, namespace in prefixes:
ElementTree.register_namespace(prefix, namespace)
else:
for prefix, namespace in prefixes:
ElementTree._namespace_map[namespace] = prefix
def register_prefix(self, nspair):
"""
Register with ElementTree a set of namespaces
:param nspair: A dictionary of prefixes and uris to use when
constructing the text representation.
:return:
"""
for prefix, uri in nspair.items():
try:
ElementTree.register_namespace(prefix, uri)
except AttributeError:
# Backwards compatibility with ET < 1.3
ElementTree._namespace_map[uri] = prefix
except ValueError:
pass
def register_namespace_prefixes():
from saml2 import md, saml, samlp
try:
from saml2 import xmlenc
from saml2 import xmldsig
except ImportError:
import xmlenc
import xmldsig
prefixes = (('saml', saml.NAMESPACE),
('samlp', samlp.NAMESPACE),
('md', md.NAMESPACE),
('ds', xmldsig.NAMESPACE),
('xenc', xmlenc.NAMESPACE))
if hasattr(ElementTree, 'register_namespace'):
for prefix, namespace in prefixes:
ElementTree.register_namespace(prefix, namespace)
else:
for prefix, namespace in prefixes:
ElementTree._namespace_map[namespace] = prefix
def __enter__(self):
from xml.etree import ElementTree
self._nsmap = ElementTree._namespace_map
self._path_cache = ElementTree.ElementPath._cache
# Copy the default namespace mapping
ElementTree._namespace_map = self._nsmap.copy()
# Copy the path cache (should be empty)
ElementTree.ElementPath._cache = self._path_cache.copy()
self.checkwarnings.__enter__()
def __exit__(self, *args):
from xml.etree import ElementTree
# Restore mapping and path cache
ElementTree._namespace_map = self._nsmap
ElementTree.ElementPath._cache = self._path_cache
self.checkwarnings.__exit__(*args)
def __enter__(self):
from xml.etree import ElementTree
self._nsmap = ElementTree._namespace_map
self._path_cache = ElementTree.ElementPath._cache
# Copy the default namespace mapping
ElementTree._namespace_map = self._nsmap.copy()
# Copy the path cache (should be empty)
ElementTree.ElementPath._cache = self._path_cache.copy()
self.checkwarnings.__enter__()
def __exit__(self, *args):
from xml.etree import ElementTree
# Restore mapping and path cache
ElementTree._namespace_map = self._nsmap
ElementTree.ElementPath._cache = self._path_cache
self.checkwarnings.__exit__(*args)
def __enter__(self):
from xml.etree import ElementTree
self._nsmap = ElementTree._namespace_map
self._path_cache = ElementTree.ElementPath._cache
# Copy the default namespace mapping
ElementTree._namespace_map = self._nsmap.copy()
# Copy the path cache (should be empty)
ElementTree.ElementPath._cache = self._path_cache.copy()
self.checkwarnings.__enter__()
def __exit__(self, *args):
from xml.etree import ElementTree
# Restore mapping and path cache
ElementTree._namespace_map = self._nsmap
ElementTree.ElementPath._cache = self._path_cache
self.checkwarnings.__exit__(*args)
def register_namespace(prefix, uri):
from xml.etree import ElementTree
# cElementTree uses ElementTree's _namespace_map, so that's ok
ElementTree._namespace_map[uri] = prefix
def __enter__(self):
from xml.etree import ElementTree
self._nsmap = ElementTree._namespace_map
self._path_cache = ElementTree.ElementPath._cache
# Copy the default namespace mapping
ElementTree._namespace_map = self._nsmap.copy()
# Copy the path cache (should be empty)
ElementTree.ElementPath._cache = self._path_cache.copy()
self.checkwarnings.__enter__()
def __exit__(self, *args):
from xml.etree import ElementTree
# Restore mapping and path cache
ElementTree._namespace_map = self._nsmap
ElementTree.ElementPath._cache = self._path_cache
self.checkwarnings.__exit__(*args)
def __enter__(self):
from xml.etree import ElementTree
self._nsmap = ElementTree._namespace_map
self._path_cache = ElementTree.ElementPath._cache
# Copy the default namespace mapping
ElementTree._namespace_map = self._nsmap.copy()
# Copy the path cache (should be empty)
ElementTree.ElementPath._cache = self._path_cache.copy()
self.checkwarnings.__enter__()
def __exit__(self, *args):
from xml.etree import ElementTree
# Restore mapping and path cache
ElementTree._namespace_map = self._nsmap
ElementTree.ElementPath._cache = self._path_cache
self.checkwarnings.__exit__(*args)
def lookup_ns_prefix_for_uri(self, node, uri):
if uri == nodes.Node.XMLNS_URI:
return 'xmlns'
result = None
# Lookup namespace URI in ET's awful global namespace/prefix registry
if hasattr(BaseET, '_namespace_map') and uri in BaseET._namespace_map:
result = BaseET._namespace_map[uri]
if result == '':
result = None
if result is None or re.match('ns\d', result):
# We either have no namespace prefix in the global mapping, in
# which case we will try looking for a matching xmlns attribute,
# or we have a namespace prefix that was probably assigned
# automatically by ElementTree and we'd rather use a
# human-assigned prefix if available.
curr_node = node
while self._is_node_an_element(curr_node):
for n, v in curr_node.attrib.items():
if v == uri:
if n.startswith('xmlns:'):
result = n.split(':')[1]
return result
elif n.startswith('{%s}' % nodes.Node.XMLNS_URI):
result = n.split('}')[1]
return result
curr_node = self.get_node_parent(curr_node)
return result