1.1 --- a/libxml2dom/__init__.py Fri Jun 20 21:40:41 2008 +0200
1.2 +++ b/libxml2dom/__init__.py Mon Dec 20 18:53:29 2004 +0000
1.3 @@ -223,10 +223,12 @@
1.4 prefix, localName = _get_prefix_and_localName(name)
1.5 if prefix is not None:
1.6 self._node.setNsProp(self._node.newNs(ns, prefix), localName, value)
1.7 - elif ns == self._node.ns().content:
1.8 + elif ns is not None and ns == self._node.ns().content:
1.9 self._node.setNsProp(self._node.ns(), localName, value)
1.10 else:
1.11 # NOTE: Needs verifying: what should happen to the namespace?
1.12 + # NOTE: This also catches the case where None is the element's
1.13 + # NOTE: namespace and is also used for the attribute.
1.14 self._node.setNsProp(None, localName, value)
1.15
1.16 def setAttribute(self, name, value):
1.17 @@ -249,8 +251,10 @@
1.18
1.19 prefix, localName = _get_prefix_and_localName(name)
1.20 _node = libxml2.newNode(localName)
1.21 - _ns = _node.newNs(ns, prefix)
1.22 - _node.setNs(_ns)
1.23 + # NOTE: Does it make sense to set the namespace if it is empty?
1.24 + if ns is not None:
1.25 + _ns = _node.newNs(ns, prefix)
1.26 + _node.setNs(_ns)
1.27 return Node(_node)
1.28
1.29 def createElement(self, name):
1.30 @@ -369,6 +373,18 @@
1.31 def __eq__(self, other):
1.32 return self._node.nodePath() == other._node.nodePath()
1.33
1.34 + # 4DOM extensions to the usual PyXML API.
1.35 + # NOTE: To be finished.
1.36 +
1.37 + def xpath(self, expr, variables=None, namespaces=None):
1.38 + context = self.ownerDocument.as_native_node().xpathNewContext()
1.39 + context.setContextNode(self.as_native_node())
1.40 + # NOTE: Discover namespaces from the node.
1.41 + for prefix, ns in (namespaces or {}).items():
1.42 + context.xpathRegisterNs(prefix, ns)
1.43 + # NOTE: May need to tidy up the context.
1.44 + return [Node(_node) for _node in context.xpathEval(expr)]
1.45 +
1.46 # Utility functions.
1.47
1.48 def createDocumentType(localName, publicId, systemId):
1.49 @@ -385,12 +401,25 @@
1.50 def parse(stream_or_string):
1.51 if hasattr(stream_or_string, "read"):
1.52 stream = stream_or_string
1.53 + return parseString(stream.read())
1.54 else:
1.55 - stream = open(stream_or_string)
1.56 - return parseString(stream.read())
1.57 + return parseFile(stream_or_string)
1.58 +
1.59 +def parseFile(s):
1.60 + # NOTE: Switching off validation and remote DTD resolution.
1.61 + context = libxml2.createFileParserCtxt(s)
1.62 + context.validate(0)
1.63 + context.ctxtUseOptions(0)
1.64 + context.parseDocument()
1.65 + return Node(context.doc())
1.66
1.67 def parseString(s):
1.68 - return Node(libxml2.parseDoc(s))
1.69 + # NOTE: Switching off validation and remote DTD resolution.
1.70 + context = libxml2.createMemoryParserCtxt(s, len(s))
1.71 + context.validate(0)
1.72 + context.ctxtUseOptions(0)
1.73 + context.parseDocument()
1.74 + return Node(context.doc())
1.75
1.76 def parseURI(uri):
1.77 return Node(libxml2.parseURI(uri))