Skip to content

Commit

Permalink
XML: Fix handling elements w/o namespace inside elements w/ ns.
Browse files Browse the repository at this point in the history
The problem occurred when namespace prefixes in Clarck notation were
stripped (default behavior) and affected both the standard etree and
lxml modes.

After the fix elements w/o ns inside elements w/ ns get `xmlns=""`
when using the standard etree. When using lxml, namespace prefixes are
preserved correctly. These fixes resolve #1784.
  • Loading branch information
pekkaklarck committed Sep 8, 2014
1 parent 789e66a commit 6515328
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions atest/robot/standard_libraries/xml/namespaces.robot
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ Saved XML is semantically same as original
Saved XML has same content as original but only default namespaces
Check Test Case ${TESTNAME}

Element without namepace inside element with namespace
Check Test Case ${TESTNAME}

Attribute namespaces are not handled
Check Test Case ${TESTNAME}
3 changes: 3 additions & 0 deletions atest/robot/standard_libraries/xml/namespaces_with_lxml.robot
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ Saved XML is semantically same as original
Saved XML has same namespaces as original
Check Test Case ${TESTNAME}

Element without namepace inside element with namespace
Check Test Case ${TESTNAME}

Attribute namespaces are not handled
Check Test Case ${TESTNAME}
7 changes: 7 additions & 0 deletions atest/testdata/standard_libraries/xml/namespaces.robot
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ Saved XML has same content as original but only default namespaces
... ${INDENT}<back>back in default</back>
... </test>

Element without namepace inside element with namespace
Save XML ${NO NS IN NS} ${OUTPUT}
Elements Should Be Equal ${NO NS IN NS} ${OUTPUT}
Saved XML Should Equal ${NO NS IN NS}
... <root xmlns="uri"><no xmlns=""><yes xmlns="uri"><no xmlns="">.</no></yes></no></root>
Element Text Should Be ${NO NS IN NS} . xpath=no/yes/no

Attribute namespaces are not handled
${elem} = Parse XML ${ATTR NS}
Test Attribute Namespace Parsing ${elem}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ Saved XML is semantically same as original
Saved XML has same namespaces as original
Saved XML Should Equal File ${NS} ${NS}

Element without namepace inside element with namespace
Save XML ${NO NS IN NS} ${OUTPUT}
Elements Should Be Equal ${NO NS IN NS} ${OUTPUT}
Saved XML Should Equal ${NO NS IN NS} ${NO NS IN NS}
Element Text Should Be ${NO NS IN NS} . xpath=no/yes/no

Attribute namespaces are not handled
${elem} = Parse XML ${ATTR NS}
Test Attribute Namespace Parsing With lxml ${elem}
Expand Down
2 changes: 1 addition & 1 deletion atest/testdata/standard_libraries/xml/xml_resource.robot
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Library String
*** Variables ***
${TEST} = ${CURDIR}/test.xml
${NS} = ${CURDIR}/namespaces.xml
${DEFAULT NS} = ${CURDIR}/default_namespaces.xml
${NO NS IN NS} = <ns:root xmlns:ns="uri"><no><ns:yes><no>.</no></ns:yes></no></ns:root>
${SIMPLE} = <root><child id="1">text</child><c2><gc /></c2></root>
${ATTR NS} = <root id="1" p:id="2" xmlns:p="xxx" />
${OUTPUT} = %{TEMPDIR}/xmllib.xml
Expand Down
3 changes: 3 additions & 0 deletions src/robot/libraries/XML.py
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,9 @@ def strip(self, elem, current_ns=None):
if ns != current_ns:
elem.attrib['xmlns'] = ns
current_ns = ns
elif current_ns:
elem.attrib['xmlns'] = ''
current_ns = None
for child in elem:
self.strip(child, current_ns)

Expand Down

0 comments on commit 6515328

Please sign in to comment.