1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/__init__.py Mon Sep 29 22:47:24 2003 +0000
1.3 @@ -0,0 +1,75 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +DOM wrapper around libxml2.
1.8 +"""
1.9 +
1.10 +import xml.dom
1.11 +
1.12 +class Attribute(object):
1.13 +
1.14 + def __init__(self, node):
1.15 + self._node = node
1.16 +
1.17 +class Node(object):
1.18 +
1.19 + _nodeTypes = {
1.20 + "attribute" : xml.dom.Node.ATTRIBUTE_NODE,
1.21 + "comment" : xml.dom.Node.COMMENT_NODE,
1.22 + "document_xml" : xml.dom.Node.DOCUMENT_NODE,
1.23 + "doctype" : xml.dom.Node.DOCUMENT_TYPE_NODE,
1.24 + "element" : xml.dom.Node.ELEMENT_NODE,
1.25 + "entity" : xml.dom.Node.ENTITY_NODE,
1.26 + "entity_ref" : xml.dom.Node.ENTITY_REFERENCE_NODE,
1.27 + "notation" : xml.dom.Node.NOTATION_NODE,
1.28 + "pi" : xml.dom.Node.PROCESSING_INSTRUCTION_NODE,
1.29 + "text" : xml.dom.Node.TEXT_NODE
1.30 + }
1.31 +
1.32 + def __init__(self, node):
1.33 + self._node = node
1.34 +
1.35 + def _ownerDocument(self):
1.36 + return self._node.doc
1.37 +
1.38 + def _nodeType(self):
1.39 + return self._nodeTypes[self._node.type]
1.40 +
1.41 + def _childNodes(self):
1.42 + child_nodes = []
1.43 + child_node = self._node.children
1.44 + while child_node is not None:
1.45 + child_nodes.append(
1.46 + self.__class__(child_node))
1.47 + child_node = child_node.next
1.48 + return child_nodes
1.49 +
1.50 + def _attributes(self):
1.51 + attributes = []
1.52 + attribute = self._node.properties
1.53 + while attribute is not None:
1.54 + attributes.append(
1.55 + Attribute(attribute))
1.56 + attribute = attribute.next
1.57 + return attributes
1.58 +
1.59 + def _tagName(self):
1.60 + if self._node.type == "element":
1.61 + return self._node.name
1.62 + else:
1.63 + return None
1.64 +
1.65 + def _namespaceURI(self):
1.66 + if self._node.type == "element":
1.67 + return self._node.ns().content
1.68 + else:
1.69 + return None
1.70 +
1.71 + nodeType = property(_nodeType)
1.72 + ownerDocument = property(_ownerDocument)
1.73 + childNodes = property(_childNodes)
1.74 + attributes = property(_attributes)
1.75 + tagName = property(_tagName)
1.76 + namespaceURI = property(_namespaceURI)
1.77 +
1.78 +# vim: tabstop=4 expandtab shiftwidth=4
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/libxml2/libxml.c.diff Mon Sep 29 22:47:24 2003 +0000
2.3 @@ -0,0 +1,11 @@
2.4 +--- libxml2-2.5.7/python/libxml.c Mon Sep 29 23:39:49 2003
2.5 ++++ libxml2-2.5.7/python/libxml.c Mon Sep 29 23:41:24 2003
2.6 +@@ -2283,7 +2283,7 @@
2.7 + return (NULL);
2.8 + node = (xmlNodePtr) PyxmlNode_Get(pyobj_node);
2.9 +
2.10 +- if ((node == NULL) || (node->type != XML_ELEMENT_NODE)) {
2.11 ++ if ((node == NULL) || ((node->type != XML_ELEMENT_NODE) && (node->type != XML_ATTRIBUTE_NODE))) {
2.12 + Py_INCREF(Py_None);
2.13 + return (Py_None);
2.14 + }
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/test.py Mon Sep 29 22:47:24 2003 +0000
3.3 @@ -0,0 +1,22 @@
3.4 +#!/usr/bin/env python
3.5 +
3.6 +import libxml2dom
3.7 +import libxml2
3.8 +#doc = libxml2.parseFile("XMLTools2/examples/zoo/zoo_schema.xml")
3.9 +doc = libxml2.parseDoc("""<?xml version="1.0" encoding="iso-8859-15"?>
3.10 +<schema xmlns="http://www.boddie.org.uk/ns/xmltools/model"
3.11 + xmlns:database="http://www.boddie.org.uk/ns/xmltools/database">
3.12 +
3.13 + <def-element name="cage" database:table="CAGE"/>
3.14 +</schema>
3.15 +""")
3.16 +d = libxml2dom.Node(doc)
3.17 +
3.18 +# This works:
3.19 +print d.childNodes[0].childNodes[1]._node.nsProp("table", 'http://www.boddie.org.uk/ns/xmltools/database')
3.20 +print doc.children.children.next.nsProp("table", 'http://www.boddie.org.uk/ns/xmltools/database')
3.21 +# This works only with an appropriate patch to libxml2 2.5.7:
3.22 +print d.childNodes[0].childNodes[1].attributes[1]._node.ns()
3.23 +# Equivalent to:
3.24 +print d.childNodes[0].childNodes[1]._node.properties.next.ns()
3.25 +print doc.children.children.next.properties.next.ns()