1.1 --- a/libxml2dom/macrolib/macrolib.py Thu Feb 02 02:52:40 2006 +0000
1.2 +++ b/libxml2dom/macrolib/macrolib.py Thu Feb 02 02:53:42 2006 +0000
1.3 @@ -61,7 +61,24 @@
1.4 # NOTE: Should raise an exception.
1.5 return None, None
1.6
1.7 +def _find_namespace_for_prefix(node, prefix):
1.8 +
1.9 + "Find the namespace definition node in the given 'node' for 'prefix'."
1.10 +
1.11 + current = libxml2mod.xmlNodeGetNsDefs(node)
1.12 + while current is not None:
1.13 + if libxml2mod.name(current) == prefix:
1.14 + return current
1.15 + current = libxml2mod.next(current)
1.16 + return None
1.17 +
1.18 def _find_namespace(node, ns, prefix):
1.19 +
1.20 + """
1.21 + Find the namespace definition node in the given 'node' for the given 'ns'
1.22 + and 'prefix'.
1.23 + """
1.24 +
1.25 new_ns = None
1.26 current = libxml2mod.xmlNodeGetNsDefs(node)
1.27 while current is not None:
1.28 @@ -76,7 +93,10 @@
1.29 return new_ns
1.30
1.31 def _check_namespace(current, ns, prefix):
1.32 - current_ns = libxml2mod.xmlNodeGetContent(current)
1.33 +
1.34 + "Check the 'current' namespace definition node against 'ns' and 'prefix'."
1.35 +
1.36 + current_ns = get_ns(current)
1.37 current_prefix = libxml2mod.name(current)
1.38 if ns == current_ns and (prefix is None or prefix == current_prefix):
1.39 return 1
1.40 @@ -84,6 +104,14 @@
1.41 return 0
1.42
1.43 def _make_namespace(node, ns, prefix, set_default=0):
1.44 +
1.45 + """
1.46 + Make a new namespace definition node within the given 'node' for 'ns',
1.47 + 'prefix', setting the default namespace on 'node' when 'prefix' is None and
1.48 + 'set_default' is set to a true value (unlike the default value for that
1.49 + parameter).
1.50 + """
1.51 +
1.52 if prefix is not None or set_default:
1.53 new_ns = libxml2mod.xmlNewNs(node, ns, prefix)
1.54 else:
1.55 @@ -143,14 +171,27 @@
1.56
1.57 def Node_attributes(node):
1.58 attributes = {}
1.59 - node = libxml2mod.properties(node)
1.60 - while node is not None:
1.61 - ns = libxml2mod.xmlNodeGetNs(node)
1.62 +
1.63 + # Include normal attributes.
1.64 +
1.65 + current = libxml2mod.properties(node)
1.66 + while current is not None:
1.67 + ns = libxml2mod.xmlNodeGetNs(current)
1.68 if ns is not None:
1.69 - attributes[(get_ns(ns), libxml2mod.name(node))] = node
1.70 + attributes[(get_ns(ns), libxml2mod.name(current))] = current
1.71 else:
1.72 - attributes[(None, libxml2mod.name(node))] = node
1.73 - node = libxml2mod.next(node)
1.74 + attributes[(None, libxml2mod.name(current))] = current
1.75 + current = libxml2mod.next(current)
1.76 +
1.77 + # Include xmlns attributes.
1.78 +
1.79 + #current = libxml2mod.xmlNodeGetNsDefs(node)
1.80 + #while current is not None:
1.81 + # ns = get_ns(current)
1.82 + # prefix = libxml2mod.name(current)
1.83 + # attributes[(xml.dom.XMLNS_NAMESPACE, "xmlns:" + prefix)] = ns # NOTE: Need a real node here.
1.84 + # current = libxml2mod.next(current)
1.85 +
1.86 return attributes
1.87
1.88 def Node_namespaceURI(node):
1.89 @@ -219,13 +260,21 @@
1.90 return libxml2mod.xmlGetIntSubset(node)
1.91
1.92 def Node_hasAttributeNS(node, ns, localName):
1.93 - return Node_getAttributeNS(node, ns, localName) is not None
1.94 + return Node_getAttributeNS(node, ns, localName) is not None or \
1.95 + _find_namespace(node, ns, localName) is not None
1.96
1.97 def Node_hasAttribute(node, name):
1.98 return Node_getAttribute(node, name) is not None
1.99
1.100 def Node_getAttributeNS(node, ns, localName):
1.101 - return to_unicode(libxml2mod.xmlGetNsProp(node, localName, ns))
1.102 + if ns == xml.dom.XMLNS_NAMESPACE:
1.103 + ns_def = _find_namespace_for_prefix(node, localName)
1.104 + if ns_def is not None:
1.105 + return get_ns(ns_def)
1.106 + else:
1.107 + return None
1.108 + else:
1.109 + return to_unicode(libxml2mod.xmlGetNsProp(node, localName, ns))
1.110
1.111 def Node_getAttribute(node, name):
1.112 return to_unicode(libxml2mod.xmlGetProp(node, name))