[libxml-devel] << operator
Sean Chittenden
sean at chittenden.org
Thu Aug 21 15:51:28 EDT 2008
>> node = XML::Node.new('foo') << XML::Node.new('bar') << "bars
>> contents"
>> puts node.parent.parent
>> <foo>
>> <bar>bars contents</bar>
>> <foo>
>
> FYI - one downside of returning the appended object as opposed to
> self is that the variable node above is the "bar contents" node.
> Thus you have to write:
>
> puts node.parent.parent.
>
> I find that surprising. It would be nice if node was the top node.
>
> Thus in the example it would be nice for << to return the child
> object in some cases and self in others. Sigh.
>
> Anyone see a way of getting the best of both worlds?
<< should operate on the contents of a node, IMHO.
So:
node1 = XML::Node.new('foo')
node1 << 'bar'
would produce '<foo>bar</foo>' and not '<foo/>bar'. However:
node2 = XML::Node.new('bar')
node1 << node2
should produce '<foo><bar/></foo>'.
If the argument to << is a string, append to the xmlNode->content via
xmlNodeAddContent(3). If, however, the argument is an XML::Node, then
use xmlAddChild(3) to add an xmlNode->child node.
+ and << should behave differently:
'node1 + node2' should produce '<foo/><bar/>'
I didn't spend a lot of time on the operators when writing this, but I
feel pretty strongly that there should be such a distinction and it
should behave differently based on the argument's data type (not that
I'm actively using the lib atm, but...). That a useful suggestion? -sc
--
Sean Chittenden
sean at chittenden.org
More information about the libxml-devel
mailing list