diff --git a/src/xmldiff/parser.py b/src/xmldiff/parser.py index 93e4069..b3fd627 100644 --- a/src/xmldiff/parser.py +++ b/src/xmldiff/parser.py @@ -71,9 +71,13 @@ def endPrefixMapping(self, prefix): def _buildTag(self, ns_name_tuple): ns_uri, local_name = ns_name_tuple if ns_uri: - el_tag = "{%s}%s" % ns_name_tuple + ns_name = [x[0] for x in self._ns_mapping.items() + if ns_uri in x[1]][0] + el_tag = "%s:%s" % (ns_name, local_name) elif self._default_ns: - el_tag = "{%s}%s" % (self._default_ns, local_name) + ns_name = [x[0] for x in self._ns_mapping.items() + if self._default_ns in x[1]][0] + el_tag = "%s:%s" % (ns_name, local_name) else: el_tag = local_name return el_tag diff --git a/tests/data/test10_ns_result b/tests/data/test10_ns_result index 24aa9ee..46b2c05 100644 --- a/tests/data/test10_ns_result +++ b/tests/data/test10_ns_result @@ -38,7 +38,7 @@ Insert mixed element with text [rename, /Tests[1]/Test[7]/Eight[1], Six] [move-first, /Tests[1]/Test[6]/@type, /Tests[1]/Test[8]] [move-first, /Tests[1]/Test[7]/@type, /Tests[1]/Test[9]] -[update, /Tests[1]/Test[3]/{urn:test_ns}Four[1]/text()[1], This WAS the fourth sentence.] +[update, /Tests[1]/Test[3]/tns:Four[1]/text()[1], This WAS the fourth sentence.] [update, /Tests[1]/Test[6]/Five[1]/text()[1], This is the] [insert-after, /Tests[1]/Test[6]/Five[1]/text()[1], @@ -57,7 +57,7 @@ and improved [append-first, /Tests[1]/Test[10]/Nine[1]/b[1], (changed) ] -[remove, /Tests[1]/{urn:test_ns}Test[1]/Two[2]] +[remove, /Tests[1]/tns:Test[1]/Two[2]] [remove, /Tests[1]/Test[2]/Three[1]] [remove, /Tests[1]/Test[4]] [remove, /Tests[1]/Test[4]] diff --git a/tests/data/test11_ns_result b/tests/data/test11_ns_result index 1968be9..8af773c 100644 --- a/tests/data/test11_ns_result +++ b/tests/data/test11_ns_result @@ -1 +1 @@ -[update, /Tests[1]/Test[1]/Nine[1]/b[1]/@{urn:test_ns}name, NineNameChanged] +[update, /Tests[1]/Test[1]/Nine[1]/b[1]/@tns:name, NineNameChanged] diff --git a/tests/test_parser.py b/tests/test_parser.py index 3fbc920..f7ff875 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -150,21 +150,21 @@ def test_tree_from_stream_with_namespace(): '/', '', [[1, - u'{urn:corp:sec}section', - u'{urn:corp:sec}section', + u'sec:section', + u'sec:section', [[1, - u'{urn:corp:sec}sectionInfo', - u'{urn:corp:sec}sectionInfo', + u'sec:sectionInfo', + u'sec:sectionInfo', [[1, - u'{urn:corp:sec}secID', - u'{urn:corp:sec}secID', + u'sec:secID', + u'sec:secID', [[4, 'text()', u'S001', [], None, 0, 1]], None, 1, 1], [1, - u'{urn:corp:sec}name', - u'{urn:corp:sec}name', + u'sec:name', + u'sec:name', [[4, 'text()', u'Sales', [], None, 0, 1]], None, 1, @@ -173,8 +173,8 @@ def test_tree_from_stream_with_namespace(): 4, 1], [1, - u'{urn:corp:sec}sectionInfo', - u'{urn:corp:sec}sectionInfo', + u'sec:sectionInfo', + u'sec:sectionInfo', [[2, u'@nameName', u'name', @@ -193,19 +193,19 @@ def test_tree_from_stream_with_namespace(): 4, 2], [1, - u'{urn:corp:sec}sectionInfo', - u'{urn:corp:sec}sectionInfo', + u'sec:sectionInfo', + u'sec:sectionInfo', [[2, - u'@{urn:corp:sec}nameName', - u'{urn:corp:sec}name', - [[3, u'@{urn:corp:sec}name', u'Gardening', [], None, 0, 0]], + u'@sec:nameName', + u'sec:name', + [[3, u'@sec:name', u'Gardening', [], None, 0, 0]], None, 1, 0], [2, - u'@{urn:corp:sec}secIDName', - u'{urn:corp:sec}secID', - [[3, u'@{urn:corp:sec}secID', u'S003', [], None, 0, 0]], + u'@sec:secIDName', + u'sec:secID', + [[3, u'@sec:secID', u'S003', [], None, 0, 0]], None, 1, 0]], @@ -249,6 +249,22 @@ def test_tree_from_lxml_with_namespace(): _nuke_parent(tree) _nuke_parent(tree_stream) + # In lxml, up to and including version 4.2.1, the namespace prefixes + # will be replaced by auto-generated namespace prefixes, ns00, ns01, etc + # If we encounter an "ns00:"" prefix, replace it. + # This code can be removed once we no longer need to run the tests with + # lxml 4.2.1 or earlier. + # This is only to fix this test, using xmldiff with these versions of + # lxml will still work, but the prefixes will be wrong. + def fix_tree(t, prefix): + t[1] = t[1].replace('ns00:', prefix) + t[2] = t[2].replace('ns00:', prefix) + for subtree in t[3]: + fix_tree(subtree, prefix) + + # lxml <= 4.2.1 + fix_tree(tree, 'sec:') + assert tree == tree_stream fname = os.path.join(HERE, 'data', 'parse', 'tal_ns.xml') @@ -261,6 +277,9 @@ def test_tree_from_lxml_with_namespace(): _nuke_parent(tree) _nuke_parent(tree_stream) + # lxml <= 4.2.1 + fix_tree(tree, 'z:') + assert tree == tree_stream