Skip to content

Commit

Permalink
The XPATHs returned when diffing with namespaces were invalid XPATHs
Browse files Browse the repository at this point in the history
"{uri:something}tag" is not valid xpaths, instead it now uses the namespace id, "something:tag"
  • Loading branch information
regebro committed Jun 14, 2018
1 parent 91e482c commit c89fd21
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
8 changes: 6 additions & 2 deletions src/xmldiff/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/data/test10_ns_result
Original file line number Diff line number Diff line change
Expand Up @@ -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],
<new/>
Expand All @@ -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]]
Expand Down
2 changes: 1 addition & 1 deletion tests/data/test11_ns_result
Original file line number Diff line number Diff line change
@@ -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]
55 changes: 37 additions & 18 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand All @@ -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]],
Expand Down Expand Up @@ -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')
Expand All @@ -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


Expand Down

0 comments on commit c89fd21

Please sign in to comment.