Does Python's ElementTree allow only one namespace mapping?
I am stumped by ElementTree's namespace handling of namespace mappings. I need to parse various trees having different default namespaces. ElementTree seems to retain the first namespace mapping I specify in find().
In the following code I expect the second pass to barf on finding D, because D is not in the namespace that gets passed to find(). Instead it does find D (which has the wrong namespace) but barfs on B (which it should find).
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
# Run code for two namespaces
namespaces = [ "http://www.example.org/X", "http://www.example.org/Y"]
for ns in namespaces:
try:
# make an XML document as a string
xmlString='''
<A xmlns="{ns}" xmlns:static="http://www.example.org/X">
<B>
<C>sam</C>
</B>
<static:D>
<C>sam</C>
</static:D>
</A>
'''.format(ns=ns)
print(xmlString)
tree = ET.fromstring(xmlString)
# See what namespace is used for the root element
print("treetag: {}".format(tree.tag))
# Find the element with the explicit namespace
elementD = tree.find("ns0:D", { "ns0":ns})
assert elementD != None, "elementD not found"
print("elementD: {}".format(elementD.tag))
# Find the element with the default namespace
elementB = tree.find("ns0:B", { "ns0":ns})
assert elementB != None, "elementB not found"
print("elementB: {}n".format(elementB.tag))
except AssertionError as e:
print repr(e)
Is there anything wrong with my code? If not, how can I force find() to use the proper namespace mapping?
Environment: Mac OS X, Python 2.7.14 |Anaconda custom (64-bit)
python namespaces elementtree
add a comment |
I am stumped by ElementTree's namespace handling of namespace mappings. I need to parse various trees having different default namespaces. ElementTree seems to retain the first namespace mapping I specify in find().
In the following code I expect the second pass to barf on finding D, because D is not in the namespace that gets passed to find(). Instead it does find D (which has the wrong namespace) but barfs on B (which it should find).
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
# Run code for two namespaces
namespaces = [ "http://www.example.org/X", "http://www.example.org/Y"]
for ns in namespaces:
try:
# make an XML document as a string
xmlString='''
<A xmlns="{ns}" xmlns:static="http://www.example.org/X">
<B>
<C>sam</C>
</B>
<static:D>
<C>sam</C>
</static:D>
</A>
'''.format(ns=ns)
print(xmlString)
tree = ET.fromstring(xmlString)
# See what namespace is used for the root element
print("treetag: {}".format(tree.tag))
# Find the element with the explicit namespace
elementD = tree.find("ns0:D", { "ns0":ns})
assert elementD != None, "elementD not found"
print("elementD: {}".format(elementD.tag))
# Find the element with the default namespace
elementB = tree.find("ns0:B", { "ns0":ns})
assert elementB != None, "elementB not found"
print("elementB: {}n".format(elementB.tag))
except AssertionError as e:
print repr(e)
Is there anything wrong with my code? If not, how can I force find() to use the proper namespace mapping?
Environment: Mac OS X, Python 2.7.14 |Anaconda custom (64-bit)
python namespaces elementtree
1
Seems like a bug that was not fixed in Python 2.7. With Python 3.7, it does barf onDas expected. See bugs.python.org/issue17011
– mzjn
Nov 22 '18 at 18:39
Ah, I hadn't found that in my searches, thanks. I'm surprised that the issue is marked as 'fixed' for Python 2.7 when it clearly isn't.
– ArtHarg
Nov 23 '18 at 10:20
1
Yes, the bug is marked as 'fixed'. But the fix was committed to the 'master' and '3.3' branches only, as far as I can tell.
– mzjn
Nov 23 '18 at 11:04
add a comment |
I am stumped by ElementTree's namespace handling of namespace mappings. I need to parse various trees having different default namespaces. ElementTree seems to retain the first namespace mapping I specify in find().
In the following code I expect the second pass to barf on finding D, because D is not in the namespace that gets passed to find(). Instead it does find D (which has the wrong namespace) but barfs on B (which it should find).
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
# Run code for two namespaces
namespaces = [ "http://www.example.org/X", "http://www.example.org/Y"]
for ns in namespaces:
try:
# make an XML document as a string
xmlString='''
<A xmlns="{ns}" xmlns:static="http://www.example.org/X">
<B>
<C>sam</C>
</B>
<static:D>
<C>sam</C>
</static:D>
</A>
'''.format(ns=ns)
print(xmlString)
tree = ET.fromstring(xmlString)
# See what namespace is used for the root element
print("treetag: {}".format(tree.tag))
# Find the element with the explicit namespace
elementD = tree.find("ns0:D", { "ns0":ns})
assert elementD != None, "elementD not found"
print("elementD: {}".format(elementD.tag))
# Find the element with the default namespace
elementB = tree.find("ns0:B", { "ns0":ns})
assert elementB != None, "elementB not found"
print("elementB: {}n".format(elementB.tag))
except AssertionError as e:
print repr(e)
Is there anything wrong with my code? If not, how can I force find() to use the proper namespace mapping?
Environment: Mac OS X, Python 2.7.14 |Anaconda custom (64-bit)
python namespaces elementtree
I am stumped by ElementTree's namespace handling of namespace mappings. I need to parse various trees having different default namespaces. ElementTree seems to retain the first namespace mapping I specify in find().
In the following code I expect the second pass to barf on finding D, because D is not in the namespace that gets passed to find(). Instead it does find D (which has the wrong namespace) but barfs on B (which it should find).
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
# Run code for two namespaces
namespaces = [ "http://www.example.org/X", "http://www.example.org/Y"]
for ns in namespaces:
try:
# make an XML document as a string
xmlString='''
<A xmlns="{ns}" xmlns:static="http://www.example.org/X">
<B>
<C>sam</C>
</B>
<static:D>
<C>sam</C>
</static:D>
</A>
'''.format(ns=ns)
print(xmlString)
tree = ET.fromstring(xmlString)
# See what namespace is used for the root element
print("treetag: {}".format(tree.tag))
# Find the element with the explicit namespace
elementD = tree.find("ns0:D", { "ns0":ns})
assert elementD != None, "elementD not found"
print("elementD: {}".format(elementD.tag))
# Find the element with the default namespace
elementB = tree.find("ns0:B", { "ns0":ns})
assert elementB != None, "elementB not found"
print("elementB: {}n".format(elementB.tag))
except AssertionError as e:
print repr(e)
Is there anything wrong with my code? If not, how can I force find() to use the proper namespace mapping?
Environment: Mac OS X, Python 2.7.14 |Anaconda custom (64-bit)
python namespaces elementtree
python namespaces elementtree
asked Nov 22 '18 at 14:25
ArtHargArtHarg
1778
1778
1
Seems like a bug that was not fixed in Python 2.7. With Python 3.7, it does barf onDas expected. See bugs.python.org/issue17011
– mzjn
Nov 22 '18 at 18:39
Ah, I hadn't found that in my searches, thanks. I'm surprised that the issue is marked as 'fixed' for Python 2.7 when it clearly isn't.
– ArtHarg
Nov 23 '18 at 10:20
1
Yes, the bug is marked as 'fixed'. But the fix was committed to the 'master' and '3.3' branches only, as far as I can tell.
– mzjn
Nov 23 '18 at 11:04
add a comment |
1
Seems like a bug that was not fixed in Python 2.7. With Python 3.7, it does barf onDas expected. See bugs.python.org/issue17011
– mzjn
Nov 22 '18 at 18:39
Ah, I hadn't found that in my searches, thanks. I'm surprised that the issue is marked as 'fixed' for Python 2.7 when it clearly isn't.
– ArtHarg
Nov 23 '18 at 10:20
1
Yes, the bug is marked as 'fixed'. But the fix was committed to the 'master' and '3.3' branches only, as far as I can tell.
– mzjn
Nov 23 '18 at 11:04
1
1
Seems like a bug that was not fixed in Python 2.7. With Python 3.7, it does barf on
D as expected. See bugs.python.org/issue17011– mzjn
Nov 22 '18 at 18:39
Seems like a bug that was not fixed in Python 2.7. With Python 3.7, it does barf on
D as expected. See bugs.python.org/issue17011– mzjn
Nov 22 '18 at 18:39
Ah, I hadn't found that in my searches, thanks. I'm surprised that the issue is marked as 'fixed' for Python 2.7 when it clearly isn't.
– ArtHarg
Nov 23 '18 at 10:20
Ah, I hadn't found that in my searches, thanks. I'm surprised that the issue is marked as 'fixed' for Python 2.7 when it clearly isn't.
– ArtHarg
Nov 23 '18 at 10:20
1
1
Yes, the bug is marked as 'fixed'. But the fix was committed to the 'master' and '3.3' branches only, as far as I can tell.
– mzjn
Nov 23 '18 at 11:04
Yes, the bug is marked as 'fixed'. But the fix was committed to the 'master' and '3.3' branches only, as far as I can tell.
– mzjn
Nov 23 '18 at 11:04
add a comment |
1 Answer
1
active
oldest
votes
You have been hit by a bug that was fixed in Python 3.3, but not in Python 2.7: https://bugs.python.org/issue17011 ("ElementPath ignores different namespace mappings for the same path expression").
When using Python 3.7, it is indeed the D element that causes an AssertionError.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433051%2fdoes-pythons-elementtree-allow-only-one-namespace-mapping%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have been hit by a bug that was fixed in Python 3.3, but not in Python 2.7: https://bugs.python.org/issue17011 ("ElementPath ignores different namespace mappings for the same path expression").
When using Python 3.7, it is indeed the D element that causes an AssertionError.
add a comment |
You have been hit by a bug that was fixed in Python 3.3, but not in Python 2.7: https://bugs.python.org/issue17011 ("ElementPath ignores different namespace mappings for the same path expression").
When using Python 3.7, it is indeed the D element that causes an AssertionError.
add a comment |
You have been hit by a bug that was fixed in Python 3.3, but not in Python 2.7: https://bugs.python.org/issue17011 ("ElementPath ignores different namespace mappings for the same path expression").
When using Python 3.7, it is indeed the D element that causes an AssertionError.
You have been hit by a bug that was fixed in Python 3.3, but not in Python 2.7: https://bugs.python.org/issue17011 ("ElementPath ignores different namespace mappings for the same path expression").
When using Python 3.7, it is indeed the D element that causes an AssertionError.
answered Nov 30 '18 at 15:57
mzjnmzjn
31.6k666154
31.6k666154
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433051%2fdoes-pythons-elementtree-allow-only-one-namespace-mapping%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Seems like a bug that was not fixed in Python 2.7. With Python 3.7, it does barf on
Das expected. See bugs.python.org/issue17011– mzjn
Nov 22 '18 at 18:39
Ah, I hadn't found that in my searches, thanks. I'm surprised that the issue is marked as 'fixed' for Python 2.7 when it clearly isn't.
– ArtHarg
Nov 23 '18 at 10:20
1
Yes, the bug is marked as 'fixed'. But the fix was committed to the 'master' and '3.3' branches only, as far as I can tell.
– mzjn
Nov 23 '18 at 11:04