[XML-SIG] py2exe and switching from PyXML 4Suite

Uche Ogbuji uche.ogbuji@fourthought.com
Sun, 15 Dec 2002 08:27:09 -0700


> On Sun, Dec 15, 2002 at 05:03:03AM -0700, Mike Brown wrote:
> > > I can't seem to find any documentation about what methods are available
> > > for cDocument objects
> > I don't think there is any yet.
> > But don't forget about Python's introspection capabilities.
> 
> Ahh.. didn't know about those, thanks.
> 
> > appendChild : Add a new child to the child list
> > cloneNode : Make a copy of a node from this document
> > createAttributeNS : Create a new attribute
> > createComment : Create a new comment node
> > createDocumentFragment : Create a new document fragment node
> > createElementNS : Create a new element
> > createNodeIterator : Create a new node iterator
> > createProcessingInstruction : Create a new processing instruction node
> > createTextNode : Create a new text node
> > hasChildNodes : Return True if the node has children
> > importNode : Make a copy of a node from any document
> > insertBefore : insert a new child to the child list
> > isSameNode : true if this node instance is the same as another
> > normalize : Combine all neighboring text child nodes
> > removeChild : Remove a node from a child list
> > replaceChild : replace a child in the child list
> 
> And now, how would I then do the equivalent of
> dom.documentElement.getElementsByTagName("tagname") with cDocument
> instead?

We didn't add a getElementsByTagName *method* to cDomlette, as I warn in

http://www.xml.com/pub/a/2002/12/11/py-xml.html

"There is also at least one warning to attach to my suggestions to use 
Domlette. The book makes frequent use of the DOM method getElementsByTagName, 
which is not supported in Domlette, for simplicity. An equivalent function is 
very easy to write for yourself. I would suggest an implementation that uses 
Python generators and I will present just such an implementation in a 
forthcoming article."

Because you're such a nice guy, I'll ditch the tease for you :-)

from __future__ import generators
from xml.dom import Node


def doc_order_iterator(node):
    yield node
    for child in node.childNodes:
        for cn in doc_order_iterator(child):
            yield cn
    return


def doc_order_iterator_filter(node, filter_func):
    if filter_func(node):
        yield node
    for child in node.childNodes:
        for cn in doc_order_iterator_filter(child, filter_func):
            yield cn
    return


def get_elements_by_tag_name(node, name):
    return doc_order_iterator_filter(node, lambda n: n.nodeType == \ 
       Node.ELEMENT_NODE and n.nodeName == name)


def get_elements_by_tag_name_ns(node, ns, local):
    return doc_order_iterator_filter(node, lambda n: n.nodeType == \ 
       Node.ELEMENT_NODE and n.namespaceURI == ns and n.localName == local)


def get_first_element_by_tag_name_ns(node, ns, local):
    return get_elements_by_tag_name_ns(node, ns, local).next()

#etc.


For a fuller discussion of the basic techniques I use here, see:

http://www-106.ibm.com/developerworks/xml/library/x-tipgenr.html

As you can see, many of the things one needs to know about PyXML and 4Suite 
are documented, but the documents are quite poorly organized.  I've put a lot 
of work into at least making the info available, and we are be putting some 
work into getting it organized in our push towards 4SUite 1.0.  Don't hesitate 
to ask, in the meantime.


-- 
Uche Ogbuji                                    Fourthought, Inc.
http://uche.ogbuji.net    http://4Suite.org    http://fourthought.com
Tour of 4Suite - http://www.xml.com/pub/a/2002/10/16/py-xml.html
Proper XML Output in Python - http://www.xml.com/pub/a/2002/11/13/py-xml.html
RSS for Python - http://www-106.ibm.com/developerworks/webservices/library/ws-p
yth11.html
Debug XSLT on the fly - http://www-106.ibm.com/developerworks/xml/library/x-deb
ugxs.html