[XML-SIG] Simple DOM API a la dom4j/jdom

Matt G. matt_g_@hotmail.com
Wed, 17 Jul 2002 06:52:26 +0000


>From: martin@v.loewis.de (Martin v. Loewis)
>To: Gerhard Häring <haering_python@gmx.de>
>CC: xml-sig@python.org
>Subject: Re: [XML-SIG] Simple DOM API a la dom4j/jdom
>Date: 17 Jul 2002 07:10:03 +0200
>
> > Now this leads me to another question. I noticed that with DOM, I can
> > only query specific elements, as there's no method like
> > getAllElements(). I really have to use SAX if I want this, right?
>
>No. I'd recommend to write a simple recursive function, or use an
>XPath expression ("*" is the XPath expression for that, I believe).
>
>The recursive function would be something like
>
>def getAllElements(node):
>   result  = [node]
>   for c node.childNodes:
>     if c.nodeType == Node.ELEMENT_NODE:
>       result.extend(getAllElements(c)
>   return result

But that's so huge, compared to something like:

    result = node.getNodes( "*" )


Note that it doesn't actually get ALL nodes in the entire tree - that would 
be "//*".

It'd be neat if an XpDom node had methods to:
    * get all matching nodes
    * get the next matching node
    * replace all matching nodes w/ X
      (could this even be an XPath expression?)
    * delete all matching nodes

I suppose you could implement the latter two by having a higher-order 
operator, which would execute a lambda with each matching node as the 
context node.

It'd be a bit like standard regexp functions, except with trees instead of 
strings.  The comment about allowing an XPath expression, for the result, is 
even potentially a nod towards regexp backrefs.


There's no reason why XPath wouldn't also work nicely w/ SAX, BTW.  For 
example, you could register SAX handlers in terms of XPath expressions (like 
XSLT templates), and even use XPath expressions to process only limited 
chunks of the tree.


Matt


_________________________________________________________________
Chat with friends online, try MSN Messenger: http://messenger.msn.com