# HG changeset patch # User paulb # Date 1064875644 0 # Node ID 270d4b24250aea6fe68eef9d6205ec7597ac0b5a # Parent a898e90cda3db91450a01ab4de86f076e43237de [project @ 2003-09-29 22:47:24 by paulb] Initial revision diff -r a898e90cda3d -r 270d4b24250a __init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/__init__.py Mon Sep 29 22:47:24 2003 +0000 @@ -0,0 +1,75 @@ +#!/usr/bin/env python + +""" +DOM wrapper around libxml2. +""" + +import xml.dom + +class Attribute(object): + + def __init__(self, node): + self._node = node + +class Node(object): + + _nodeTypes = { + "attribute" : xml.dom.Node.ATTRIBUTE_NODE, + "comment" : xml.dom.Node.COMMENT_NODE, + "document_xml" : xml.dom.Node.DOCUMENT_NODE, + "doctype" : xml.dom.Node.DOCUMENT_TYPE_NODE, + "element" : xml.dom.Node.ELEMENT_NODE, + "entity" : xml.dom.Node.ENTITY_NODE, + "entity_ref" : xml.dom.Node.ENTITY_REFERENCE_NODE, + "notation" : xml.dom.Node.NOTATION_NODE, + "pi" : xml.dom.Node.PROCESSING_INSTRUCTION_NODE, + "text" : xml.dom.Node.TEXT_NODE + } + + def __init__(self, node): + self._node = node + + def _ownerDocument(self): + return self._node.doc + + def _nodeType(self): + return self._nodeTypes[self._node.type] + + def _childNodes(self): + child_nodes = [] + child_node = self._node.children + while child_node is not None: + child_nodes.append( + self.__class__(child_node)) + child_node = child_node.next + return child_nodes + + def _attributes(self): + attributes = [] + attribute = self._node.properties + while attribute is not None: + attributes.append( + Attribute(attribute)) + attribute = attribute.next + return attributes + + def _tagName(self): + if self._node.type == "element": + return self._node.name + else: + return None + + def _namespaceURI(self): + if self._node.type == "element": + return self._node.ns().content + else: + return None + + nodeType = property(_nodeType) + ownerDocument = property(_ownerDocument) + childNodes = property(_childNodes) + attributes = property(_attributes) + tagName = property(_tagName) + namespaceURI = property(_namespaceURI) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r a898e90cda3d -r 270d4b24250a libxml2/libxml.c.diff --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libxml2/libxml.c.diff Mon Sep 29 22:47:24 2003 +0000 @@ -0,0 +1,11 @@ +--- libxml2-2.5.7/python/libxml.c Mon Sep 29 23:39:49 2003 ++++ libxml2-2.5.7/python/libxml.c Mon Sep 29 23:41:24 2003 +@@ -2283,7 +2283,7 @@ + return (NULL); + node = (xmlNodePtr) PyxmlNode_Get(pyobj_node); + +- if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) { ++ if ((node == NULL) || ((node->type != XML_ELEMENT_NODE) && (node->type != XML_ATTRIBUTE_NODE))) { + Py_INCREF(Py_None); + return (Py_None); + } diff -r a898e90cda3d -r 270d4b24250a test.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test.py Mon Sep 29 22:47:24 2003 +0000 @@ -0,0 +1,22 @@ +#!/usr/bin/env python + +import libxml2dom +import libxml2 +#doc = libxml2.parseFile("XMLTools2/examples/zoo/zoo_schema.xml") +doc = libxml2.parseDoc(""" + + + + +""") +d = libxml2dom.Node(doc) + +# This works: +print d.childNodes[0].childNodes[1]._node.nsProp("table", 'http://www.boddie.org.uk/ns/xmltools/database') +print doc.children.children.next.nsProp("table", 'http://www.boddie.org.uk/ns/xmltools/database') +# This works only with an appropriate patch to libxml2 2.5.7: +print d.childNodes[0].childNodes[1].attributes[1]._node.ns() +# Equivalent to: +print d.childNodes[0].childNodes[1]._node.properties.next.ns() +print doc.children.children.next.properties.next.ns()