@@ -30,7 +30,6 @@ class OneLogin_Saml2_XML(object):
3030 dump = staticmethod (etree .dump )
3131 make_root = staticmethod (etree .Element )
3232 make_child = staticmethod (etree .SubElement )
33- cleanup_namespaces = staticmethod (etree .cleanup_namespaces )
3433
3534 @staticmethod
3635 def to_string (xml , ** kwargs ):
@@ -123,6 +122,28 @@ def query(dom, query, context=None):
123122 else :
124123 return context .xpath (query , namespaces = OneLogin_Saml2_Constants .NSMAP )
125124
125+ @staticmethod
126+ def cleanup_namespaces (tree_or_element , top_nsmap = None , keep_ns_prefixes = None ):
127+ """
128+ Keeps the xmlns:xs namespace intact when etree.cleanup_namespaces is invoked.
129+ :param tree_or_element: An XML tree or element
130+ :type tree_or_element: etree.Element
131+ :param top_nsmap: A mapping from namespace prefixes to namespace URIs
132+ :type top_nsmap: dict
133+ :param keep_ns_prefixes: List of prefixes that should not be removed as part of the cleanup
134+ :type keep_ns_prefixes: list
135+ :returns: An XML tree or element
136+ :rtype: etree.Element
137+ """
138+ all_prefixes_to_keep = [
139+ OneLogin_Saml2_Constants .NS_PREFIX_XS
140+ ]
141+
142+ if keep_ns_prefixes :
143+ all_prefixes_to_keep = list (set (all_prefixes_to_keep .extend (keep_ns_prefixes )))
144+
145+ return etree .cleanup_namespaces (tree_or_element , keep_ns_prefixes = all_prefixes_to_keep )
146+
126147 @staticmethod
127148 def extract_tag_text (xml , tagname ):
128149 open_tag = compat .to_bytes ("<%s" % tagname )
0 commit comments