Since your code uses parseString and getElementsByTagName
, I'm assuming you are using minidom. In that case, try:
dom.getElementsByTagNameNS('*', 'Scopes')
It doesn't say so in the docs, but if you look in the source code for xml/dom/minidom.py
, you'll see getElementsByTagNameNS
calls _get_elements_by_tagName_ns_helper
which is defined like this:
def _get_elements_by_tagName_ns_helper(parent, nsURI, localName, rc):
for node in parent.childNodes:
if node.nodeType == Node.ELEMENT_NODE:
if ((localName == "*" or node.localName == localName) and
(nsURI == "*" or node.namespaceURI == nsURI)):
rc.append(node)
_get_elements_by_tagName_ns_helper(node, nsURI, localName, rc)
return rc
Notice that when nsURI
equals *
, only the localName
needs to match.
For example,
import xml.dom.minidom as minidom
content = '''<root xmlns:f="foo"><f:test/><f:test/></root>'''
dom = minidom.parseString(content)
for n in dom.getElementsByTagNameNS('*', 'test'):
print(n.toxml())
# <f:test/>
# <f:test/>