Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 984158d

Browse files
committed
Patch #432117: Record namespaces in the DOM tree using the DOM xmlns prefix.
1 parent b311ad5 commit 984158d

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

Lib/xml/dom/pulldom.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,23 @@ def setDocumentLocator(self, locator):
4444
self._locator = locator
4545

4646
def startPrefixMapping(self, prefix, uri):
47+
if not hasattr(self, '_xmlns_attrs'):
48+
self._xmlns_attrs = []
49+
self._xmlns_attrs.append((prefix or 'xmlns', uri))
4750
self._ns_contexts.append(self._current_context.copy())
4851
self._current_context[uri] = prefix or ''
4952

5053
def endPrefixMapping(self, prefix):
5154
self._current_context = self._ns_contexts.pop()
5255

5356
def startElementNS(self, name, tagName , attrs):
57+
# Retrieve xml namespace declaration attributes.
58+
xmlns_uri = 'http://www.w3.org/2000/xmlns/'
59+
xmlns_attrs = getattr(self, '_xmlns_attrs', None)
60+
if xmlns_attrs is not None:
61+
for aname, value in xmlns_attrs:
62+
attrs._attrs[(xmlns_uri, aname)] = value
63+
self._xmlns_attrs = []
5464
uri, localname = name
5565
if uri:
5666
# When using namespaces, the reader may or may not
@@ -76,7 +86,14 @@ def startElementNS(self, name, tagName , attrs):
7686

7787
for aname,value in attrs.items():
7888
a_uri, a_localname = aname
79-
if a_uri:
89+
if a_uri == xmlns_uri:
90+
if a_localname == 'xmlns':
91+
qname = a_localname
92+
else:
93+
qname = 'xmlns:' + a_localname
94+
attr = self.document.createAttributeNS(a_uri, qname)
95+
node.setAttributeNodeNS(attr)
96+
elif a_uri:
8097
prefix = self._current_context[a_uri]
8198
if prefix:
8299
qname = prefix + ":" + a_localname

0 commit comments

Comments
 (0)