Nested XML trees

I am trying to append/insert tree values in to tree nodes.
The simplest example of this I can come up w/ is:
put tree(“foo”:tree(“bar”:1))

This should result in the following tree:

1

Instead, it results in the following error:
NSInternalInconsistencyException - Cannot add a child that has a parent; detach or copy first

Why do I get this error?

Thanks!

Pv

You’ve run into a small bug with the property list to tree conversion – basically, it isn’t expecting something that’s already a tree as a value in a property list. That will be fixed in the next version of Eggplant.

Meanwhile, you can simply leave off the nested call to the tree() function:

put tree(foo: (bar:1) )

While converting a property list to a tree, nested property lists will be converted too, so that should give you what you want. (Note: quotes aren’t needed around property names unless they contain special characters.)

Thanks! I look forward to the patch.

Another possible bug:

put tree(())

Should probably output “<></>” (same as “an empty tree”)
Instead, outputs “()”

Pv

And another possible bug:

set foo to "<html><body>hello</body></html>"
put foo
put tree(foo)
put tree(string:foo)

In “put tree(string:foo)”, it appears to be trying to parse foo as a tree.

Pv

Here is a workaround I am using:

set html to "<html><body>hello</body></html>"
set xml to tree(value:empty)
insert (html as text) into xml
put xml

Pv

[quote=“pv”]Another possible bug:

put tree(())

Should probably output “<></>” (same as “an empty tree”)
Instead, outputs “()”[/quote]
Yes, that’s a good point. Thanks for pointing that out. It’s treating the empty list as text, but converting any list to a tree should really treat it as a sequence of nodes within an unnamed element, as you suggest. This will be fixed for a future release.

[quote=“pv”]And another possible bug:

set foo to "<html><body>hello</body></html>"
put foo
put tree(foo)
put tree(string:foo)

In “put tree(string:foo)”, it appears to be trying to parse foo as a tree.[/quote]
This isn’t really a bug. The conversion from a property list to a tree operates in a “deep” fashion, doing its best to interpret each value as a possible subtree. So if a value is proper XML it is converted to a subtree at that point.

I can see how that isn’t what you really want in this case, since you just want the html text stored in the tree. For now, you’ve already found one possible way to prevent this. Here’s another way to explicitly specify that you want a text node at some point in a tree, using the special “_text” property:

set html to "<html><body>hello</body></html>" 
set xml to tree(value:(_text:html)) 
put xml

Hopefully one of these approaches will work well for your needs.