# HG changeset patch # User paulb # Date 1134671143 0 # Node ID 31a75d7c25a34f9854f4e0db35ba58ca67fc6a96 # Parent fd05591c8febc5468bf273e7f454a3680248223d [project @ 2005-12-15 18:25:43 by paulb] Added changes to setAttributeNS to help fix the namespace issues with unprefixed attribute names. A naive prefix generating function is included. Updated release information. diff -r fd05591c8feb -r 31a75d7c25a3 libxml2dom/macrolib/__init__.py --- a/libxml2dom/macrolib/__init__.py Thu Dec 15 18:24:32 2005 +0000 +++ b/libxml2dom/macrolib/__init__.py Thu Dec 15 18:25:43 2005 +0000 @@ -20,7 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -__version__ = "0.3.3" +__version__ = "0.3.4" # Expose all functions here. diff -r fd05591c8feb -r 31a75d7c25a3 libxml2dom/macrolib/macrolib.py --- a/libxml2dom/macrolib/macrolib.py Thu Dec 15 18:24:32 2005 +0000 +++ b/libxml2dom/macrolib/macrolib.py Thu Dec 15 18:25:43 2005 +0000 @@ -78,7 +78,7 @@ def _check_namespace(current, ns, prefix): current_ns = libxml2mod.xmlNodeGetContent(current) current_prefix = libxml2mod.name(current) - if ns == current_ns and prefix == current_prefix: + if ns == current_ns and (prefix is None or prefix == current_prefix): return 1 else: return 0 @@ -90,6 +90,20 @@ new_ns = None return new_ns +def _get_invented_prefix(node, ns): + current = libxml2mod.xmlNodeGetNsDefs(node) + prefixes = [] + while current is not None: + current_prefix = libxml2mod.name(current) + prefixes.append(current_prefix) + current = libxml2mod.next(current) + i = 0 + while 1: + prefix = "NS%d" % i + if prefix not in prefixes: + return prefix + i += 1 + _nodeTypes = { "attribute" : xml.dom.Node.ATTRIBUTE_NODE, "comment" : xml.dom.Node.COMMENT_NODE, @@ -238,8 +252,13 @@ # For non-xmlns attributes, we find or make a namespace declaration and then # set an attribute. elif ns is not None: + # Look for a suitable namespace. new_ns = _find_namespace(node, ns, prefix) + # Create a declaration if no suitable one was found. if new_ns is None: + # Invent a prefix for unprefixed attributes with namespaces. + if prefix is None: + prefix = _get_invented_prefix(node, ns) new_ns = _make_namespace(node, ns, prefix, set_default=0) libxml2mod.xmlSetNsProp(node, new_ns, localName, value) else: