[libxml-devel] << operator

Sean Chittenden sean at chittenden.org
Thu Aug 21 17:10:55 EDT 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

>> XML::Node.new('foo') << XML::Node.new('bar') << XML::Node.new('baz')
>> Should produce:
>> <foo><bar><baz/></bar></foo>
>> XML::Node<<(arg) should return the argument being appended *ONLY  
>> IF* the argument is an XML::Node, else it should return the xmlNode  
>> that had its content modified (e.g. XML::Attributes and Strings).
>> That should solve this problem.
>
> So in this case the first << returns the 'bar' node and the second  
> << returns the baz node.
>
> Thus:
>
> node = XML::Node.new('foo') << XML::Node.new('bar') <<  
> XML::Node.new('baz')
>
> node is baz.  Which is annoying, because most of the time I imagine  
> you want foo (at least I want foo).  Thus, returning the appended  
> argument is great, until the very end, where you want the original  
> node...


ahahahaa...  doh!  Annoying and equally confusing.  Since this is a  
fragment, what about:

node = (XML::Node.new('foo') << XML::Node.new('bar') <<  
XML::Node.new('baz')).root

or

node = (XML::Node.new('foo') << XML::Node.new('bar') <<  
XML::Node.new('baz')).parent!

where "parent!" recursively searches for the parent node that has a  
null parent (??).  I'm guessing the most common situation is going to  
be finding a node in a document, then modifying it.

Preserving an easy to use syntax for chaining nodes when building a  
fragment is more important than an assignment.  Is that an acceptable  
argument to everyone?  Feel free to debate this point!

- -sc

- --
Sean Chittenden
sean at chittenden.org



-----BEGIN PGP SIGNATURE-----

iEYEARECAAYFAkit2eAACgkQTrydwWwuXhawEQCff1y9cm8HIxInKCz/318Cy8xx
NKAAmwUs622pU+ScMyLU/oIGsCQ6nRrD
=UOJa
-----END PGP SIGNATURE-----


More information about the libxml-devel mailing list