From 6515328eea0a522912bb32313a9f96ac569675ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pekka=20Kl=C3=A4rck?= Date: Mon, 8 Sep 2014 11:25:25 +0300 Subject: [PATCH] XML: Fix handling elements w/o namespace inside elements w/ ns. 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. --- atest/robot/standard_libraries/xml/namespaces.robot | 3 +++ .../standard_libraries/xml/namespaces_with_lxml.robot | 3 +++ atest/testdata/standard_libraries/xml/namespaces.robot | 7 +++++++ .../standard_libraries/xml/namespaces_with_lxml.robot | 6 ++++++ atest/testdata/standard_libraries/xml/xml_resource.robot | 2 +- src/robot/libraries/XML.py | 3 +++ 6 files changed, 23 insertions(+), 1 deletion(-) diff --git a/atest/robot/standard_libraries/xml/namespaces.robot b/atest/robot/standard_libraries/xml/namespaces.robot index 9e15b277960..785893edc36 100644 --- a/atest/robot/standard_libraries/xml/namespaces.robot +++ b/atest/robot/standard_libraries/xml/namespaces.robot @@ -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} diff --git a/atest/robot/standard_libraries/xml/namespaces_with_lxml.robot b/atest/robot/standard_libraries/xml/namespaces_with_lxml.robot index 1a8fecfddf1..334dd2e1a43 100644 --- a/atest/robot/standard_libraries/xml/namespaces_with_lxml.robot +++ b/atest/robot/standard_libraries/xml/namespaces_with_lxml.robot @@ -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} diff --git a/atest/testdata/standard_libraries/xml/namespaces.robot b/atest/testdata/standard_libraries/xml/namespaces.robot index 52c85509f7c..ce66886dcf4 100644 --- a/atest/testdata/standard_libraries/xml/namespaces.robot +++ b/atest/testdata/standard_libraries/xml/namespaces.robot @@ -64,6 +64,13 @@ Saved XML has same content as original but only default namespaces ... ${INDENT}back in default ... +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} + ... . + 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} diff --git a/atest/testdata/standard_libraries/xml/namespaces_with_lxml.robot b/atest/testdata/standard_libraries/xml/namespaces_with_lxml.robot index 14092b34634..9e6c0909018 100644 --- a/atest/testdata/standard_libraries/xml/namespaces_with_lxml.robot +++ b/atest/testdata/standard_libraries/xml/namespaces_with_lxml.robot @@ -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} diff --git a/atest/testdata/standard_libraries/xml/xml_resource.robot b/atest/testdata/standard_libraries/xml/xml_resource.robot index 25310408b70..8ca9e6c3aae 100644 --- a/atest/testdata/standard_libraries/xml/xml_resource.robot +++ b/atest/testdata/standard_libraries/xml/xml_resource.robot @@ -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} = . ${SIMPLE} = text ${ATTR NS} = ${OUTPUT} = %{TEMPDIR}/xmllib.xml diff --git a/src/robot/libraries/XML.py b/src/robot/libraries/XML.py index abc01261418..e208d5d65d8 100644 --- a/src/robot/libraries/XML.py +++ b/src/robot/libraries/XML.py @@ -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)