本文整理汇总了Python中lxml.etree.cleanup_namespaces方法的典型用法代码示例。如果您正苦于以下问题:Python etree.cleanup_namespaces方法的具体用法?Python etree.cleanup_namespaces怎么用?Python etree.cleanup_namespaces使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lxml.etree
的用法示例。
在下文中一共展示了etree.cleanup_namespaces方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_add_netconf_attr_prefix
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import cleanup_namespaces [as 别名]
def test_add_netconf_attr_prefix(self):
"""Handling of netconf attributes with and without prefix."""
elem = self.rpcbld.netconf_element('rpc')
self.rpcbld.add_netconf_attr(elem, 'message-id', 101)
et.cleanup_namespaces(
elem, keep_ns_prefixes=sorted(self.rpcbld.keep_prefixes))
self.assertEqual("""\
<nc:rpc xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" \
nc:message-id="101"/>
""", et.tostring(elem, encoding='unicode', pretty_print=True))
elem = self.rpcbld_minimal.netconf_element('rpc')
self.rpcbld_minimal.add_netconf_attr(elem, 'message-id', 101)
et.cleanup_namespaces(
elem, keep_ns_prefixes=sorted(self.rpcbld_minimal.keep_prefixes))
self.assertEqual("""\
<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="101"/>
""", et.tostring(elem, encoding='unicode', pretty_print=True))
示例2: format
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import cleanup_namespaces [as 别名]
def format(self, diff, orig_tree, differ=None):
# Make a new tree, both because we want to add the diff namespace
# and also because we don't want to modify the original tree.
result = deepcopy(orig_tree)
if isinstance(result, etree._ElementTree):
root = result.getroot()
else:
root = result
etree.register_namespace(DIFF_PREFIX, DIFF_NS)
for action in diff:
self.handle_action(action, root)
self.finalize(root)
etree.cleanup_namespaces(result, top_nsmap={DIFF_PREFIX: DIFF_NS})
return self.render(result)
示例3: serialize
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import cleanup_namespaces [as 别名]
def serialize(self, sink: Union[IO, str], cas: Cas, pretty_print=True):
xmi_attrs = {"{http://www.omg.org/XMI}version": "2.0"}
root = etree.Element(etree.QName(self._nsmap["xmi"], "XMI"), nsmap=self._nsmap, **xmi_attrs)
self._serialize_cas_null(root)
# Find all fs, even the ones that are not directly added to a sofa
for fs in sorted(cas._find_all_fs(), key=lambda a: a.xmiID):
self._serialize_feature_structure(cas, root, fs)
for sofa in cas.sofas:
self._serialize_sofa(root, sofa)
for view in cas.views:
self._serialize_view(root, view)
doc = etree.ElementTree(root)
etree.cleanup_namespaces(doc, top_nsmap=self._nsmap)
doc.write(sink, xml_declaration=True, pretty_print=pretty_print)
示例4: to_string
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import cleanup_namespaces [as 别名]
def to_string(xml, **kwargs):
"""
Serialize an element to an encoded string representation of its XML tree.
:param xml: The root node
:type xml: str|bytes|xml.dom.minidom.Document|etree.Element
:returns: string representation of xml
:rtype: string
"""
if isinstance(xml, OneLogin_Saml2_XML._text_class):
return xml
if isinstance(xml, OneLogin_Saml2_XML._element_class):
OneLogin_Saml2_XML.cleanup_namespaces(xml)
return OneLogin_Saml2_XML._unparse_etree(xml, **kwargs)
raise ValueError("unsupported type %r" % type(xml))
示例5: cleanup_namespaces
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import cleanup_namespaces [as 别名]
def cleanup_namespaces(tree_or_element, top_nsmap=None, keep_ns_prefixes=None):
"""
Keeps the xmlns:xs namespace intact when etree.cleanup_namespaces is invoked.
:param tree_or_element: An XML tree or element
:type tree_or_element: etree.Element
:param top_nsmap: A mapping from namespace prefixes to namespace URIs
:type top_nsmap: dict
:param keep_ns_prefixes: List of prefixes that should not be removed as part of the cleanup
:type keep_ns_prefixes: list
:returns: An XML tree or element
:rtype: etree.Element
"""
all_prefixes_to_keep = [
OneLogin_Saml2_Constants.NS_PREFIX_XS,
OneLogin_Saml2_Constants.NS_PREFIX_XSI,
OneLogin_Saml2_Constants.NS_PREFIX_XSD
]
if keep_ns_prefixes:
all_prefixes_to_keep = list(set(all_prefixes_to_keep.extend(keep_ns_prefixes)))
return etree.cleanup_namespaces(tree_or_element, keep_ns_prefixes=all_prefixes_to_keep)
示例6: main
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import cleanup_namespaces [as 别名]
def main():
data = sys.stdin.readlines()
payload = data[0]
payload_dict = json.loads(payload)
if "response" in payload_dict and "body" in payload_dict["response"]:
body = payload_dict["response"]["body"]
try:
root = objectify.fromstring(str(body))
ns = "{http://ws.cdyne.com/}"
logging.debug("transforming")
ipe = ns + "ResolveIPResponse"
ipt = ns + "ResolveIPResult"
root.Body[ipe][ipt].City = "New York"
objectify.deannotate(root.Body[ipe][ipt].City)
etree.cleanup_namespaces(root.Body[ipe][ipt].City)
payload_dict["response"]["body"] = etree.tostring(root)
logging.debug(etree.tostring(root))
except Exception:
pass
print(json.dumps(payload_dict))
示例7: remove_namespaces
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import cleanup_namespaces [as 别名]
def remove_namespaces(self):
"""
Remove all namespaces, allowing to traverse the document using
namespace-less xpaths. See :ref:`removing-namespaces`.
"""
for el in self.root.iter('*'):
if el.tag.startswith('{'):
el.tag = el.tag.split('}', 1)[1]
# loop on element attributes also
for an in el.attrib.keys():
if an.startswith('{'):
el.attrib[an.split('}', 1)[1]] = el.attrib.pop(an)
# remove namespace declarations
etree.cleanup_namespaces(self.root)
示例8: remove_namespaces
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import cleanup_namespaces [as 别名]
def remove_namespaces(self):
"""
Remove all namespaces, allowing to traverse the document using
namespace-less xpaths. See :ref:`removing-namespaces`.
"""
for el in self.root.iter('*'):
if el.tag.startswith('{'):
el.tag = el.tag.split('}', 1)[1]
# loop on element attributes also
for an in el.attrib.keys():
if an.startswith('{'):
el.attrib[an.split('}', 1)[1]] = el.attrib.pop(an)
# remove namespace declarations
etree.cleanup_namespaces(self.root)
示例9: _save_script
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import cleanup_namespaces [as 别名]
def _save_script(self):
# objectify.deannotate(self.dragon)
etree.cleanup_namespaces(self.dragon)
self.drs_name = 'Dragon_script.drs'
with open(os.path.join(self.output_directory, self.drs_name), 'w') as outfile:
outfile.write(etree.tostring(self.dragon, pretty_print=True).decode())
示例10: printout
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import cleanup_namespaces [as 别名]
def printout(self):
objectify.deannotate(self.dragon)
etree.cleanup_namespaces(self.dragon)
print(objectify.dump(self.dragon))