# HG changeset patch # User paulb # Date 1116185143 0 # Node ID 4704936cfcfc8fdd78d7b7534f61955241dd4078 # Parent a16b1aa8c9130266c7e1a5f88f78ab7f332752b8 [project @ 2005-05-15 19:25:43 by paulb] Fixed parentNode, the result of xpath (to permit none node list results), added convenience methods and node type values. Added an adoptNodes function - useful when acquiring stylesheet result documents and taking control of their management. diff -r a16b1aa8c913 -r 4704936cfcfc libxml2dom/__init__.py --- a/libxml2dom/__init__.py Sun May 15 19:24:38 2005 +0000 +++ b/libxml2dom/__init__.py Sun May 15 19:25:43 2005 +0000 @@ -6,7 +6,6 @@ __version__ = "0.2" -import xml.dom import libxml2 from libxml2dom.macrolib import * from libxml2dom.macrolib import \ @@ -75,6 +74,17 @@ A DOM-style wrapper around libxml2mod objects. """ + ATTRIBUTE_NODE = xml.dom.Node.ATTRIBUTE_NODE + COMMENT_NODE = xml.dom.Node.COMMENT_NODE + DOCUMENT_NODE = xml.dom.Node.DOCUMENT_NODE + DOCUMENT_TYPE_NODE = xml.dom.Node.DOCUMENT_TYPE_NODE + ELEMENT_NODE = xml.dom.Node.ELEMENT_NODE + ENTITY_NODE = xml.dom.Node.ENTITY_NODE + ENTITY_REFERENCE_NODE = xml.dom.Node.ENTITY_REFERENCE_NODE + NOTATION_NODE = xml.dom.Node.NOTATION_NODE + PROCESSING_INSTRUCTION_NODE = xml.dom.Node.PROCESSING_INSTRUCTION_NODE + TEXT_NODE = xml.dom.Node.TEXT_NODE + def __init__(self, node, ownerDocument=None): self._node = node self.ownerDocument = ownerDocument @@ -113,7 +123,7 @@ return Node_localName(self._node) def _parentNode(self): - return Node_parentNode(self._node) + return Node(Node_parentNode(self._node), self.ownerDocument) def _previousSibling(self): return Node(Node_previousSibling(self._node), self.ownerDocument) @@ -224,7 +234,22 @@ # NOTE: To be finished. def xpath(self, expr, variables=None, namespaces=None): - return [Node(_node, self.ownerDocument) for _node in Node_xpath(self._node, expr, variables, namespaces)] + result = Node_xpath(self._node, expr, variables, namespaces) + if hasattr(result, "len"): + return [Node(_node, self.ownerDocument) for _node in result] + else: + return result + + # Convenience methods. + + def toString(self, encoding=None): + return toString(self, encoding) + + def toStream(self, stream, encoding=None): + toStream(self, stream, encoding) + + def toFile(self, f, encoding=None): + toFile(self, f, encoding) # Document housekeeping mechanisms. @@ -278,4 +303,13 @@ def toFile(node, f, encoding=None): Node_toFile(node.as_native_node(), f, encoding) +def adoptNodes(nodes): + if len(nodes) == 0: + return [] + doc = Document(libxml2mod.doc(nodes[0])) + results = [] + for node in nodes: + results.append(Node(node, doc)) + return results + # vim: tabstop=4 expandtab shiftwidth=4