[libxml-devel] Proposed API changes for working with child nodes and attributes

Charlie Savage cfis at savagexi.com
Tue Jul 8 12:26:37 EDT 2008


I've noticed two oddities in the libxml api - dealing with child 
elements and attributes.  In both cases, the returned value is a single 
item (an attribute or node) that then provides methods to get to the 
next item.

So:

some_node = doc.find('/foo')

child = some_node.children
while child
   ... do stuff
   child = child.next
end

There is also a bit of ruby syntactic sugar that defines an each method, 
so you can do:

some_node = doc.find('/foo')

some_node.children.each do |node|
   ... do stuff
end

Thus node.children is a XML::Node that acts both as a single node and 
also a collection.  The same is true for attributes.

For anyone who works with the DOM's built into browsers, or follows the 
W3C standards 
(\http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html), 
this is a bit weird and non-intuitive.

I'd prefer that node.children becomes node.first and node.children 
returns an array of child nodes.  We'd then add an each_child method to 
XML::Node (based on node.first and node.next) for quick and easy iteration.

some_node = doc.find('/foo')

child_nodes = some_node.children
child_nodes.class == "Array"

some_node.each_child do |node|
   ... do stuff
end


And for attributes, I vote that libxml copies REXML's interface:

http://www.germane-software.com/software/rexml/doc/classes/REXML/Attributes.html

Thoughts?

Charlie
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3237 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://rubyforge.org/pipermail/libxml-devel/attachments/20080708/b7bfbcad/attachment.bin>


More information about the libxml-devel mailing list