Hi there, I've been experimenting with manipulating XML documents in Zope. At first, it seemed to work: I made an XML document called 'testdoc' with the following content: <?xml version="1.0"?> <list> <li>Zero</li> <li>One</li> <li>Two</li> <li>Three</li> </list> And I made another one called 'someElement' with the following content: <?xml version="1.0"?> <li>Another list item</li> Then I made a DTML method called 'testInsert' (actually I'm appending), doing the following: <!--#var standard_html_header--> <!--#call "testdoc[0].appendChild(someElement[0])"--> <p>Done insertion</p> <!--#var standard_html_footer--> This indeed adds the new element to the <list> in the testdoc. However, the <li>Another list item</li> disappears from someElement! Is this what should happen? A clue can be found in the XML Document source (Node.py): def appendChild(self, newChild): """ Adds the node newChild to the end of the list of children of this node. If the newChild is already in the tree, it is first removed. """ But in this case 'newChild' is in *another* tree and it is removed as well. Is this intentionally? It seems as if it would be difficult to build trees now, or am I doing something wrong and shouldn't I use XML Documents for XML fragments that I want to insert? I think it may in fact be hard to determine if a cross-tree append is taking place, efficiently at least; right now it is only checked if the newChild to be inserted has a parentNode at all (and removes it from that parent node if so). Regards, Martijn
Martijn Faassen wrote:
def appendChild(self, newChild): """ Adds the node newChild to the end of the list of children of this node. If the newChild is already in the tree, it is first removed. """
But in this case 'newChild' is in *another* tree and it is removed as well. Is this intentionally?
I don't think that the standard DOM API even has a provision for moving nodes from one document to another. The more important point is that a single element cannot have two parents. You should clone it. -- Paul Prescod - ISOGEN Consulting Engineer speaking for only himself http://itrc.uwaterloo.ca/~papresco To me programming is more than an important practical art. It is also a gigantic undertaking in the foundations of knowledge. - Grace Hopper
Paul Prescod wrote:
Martijn Faassen wrote:
def appendChild(self, newChild): """ Adds the node newChild to the end of the list of children of this node. If the newChild is already in the tree, it is first removed. """
But in this case 'newChild' is in *another* tree and it is removed as well. Is this intentionally?
I don't think that the standard DOM API even has a provision for moving nodes from one document to another.
This is possible, thought the provision (in the DOM spec): Adds the node newChild to the end of the list of children of this node. If the newChild is already in the tree, it is first removed. would imply that it is possible for a newChild to be added that is not already in the tree. But apparently it can't come from another tree, as that is cloning it, really?
The more important point is that a single element cannot have two parents. You should clone it.
Yeah, but that isn't supported by XML Document yet. :) Perhaps the newer version is, and if it isn't, I'll look at trying to add it (first I want to see the new version has it, but then I need to be able to get my hands on it). Regards, Martijn
At 08:35 PM 7/27/99 +0200, Martijn Faassen wrote:
Paul Prescod wrote:
The more important point is that a single element cannot have two parents. You should clone it.
Thanks for this clarification. I'd like XML Document to adhere to the DOM spec as much as possible.
Yeah, but that isn't supported by XML Document yet. :) Perhaps the newer version is, and if it isn't, I'll look at trying to add it (first I want to see the new version has it, but then I need to be able to get my hands on it).
True, clone along with many other DOM write methods isn't supported by XML Document yet. I bet it would be real simple to implement. BTW, we should have CVS access to XML Document soon... There are some interesting issues related to the idea of Document nodes in Zope now that with ZDOM all Zope objects are considered DOM elements. Vanilla Zope objects are elements with the Zope root object being the Document node. However XML Document objects want to be their own documents. Hmm. I'm not sure what kind of problems this will present... -Amos
Martijn Faassen wrote:
This is possible, thought the provision (in the DOM spec):
Adds the node newChild to the end of the list of children of this node. If the newChild is already in the tree, it is first removed.
Right. Since the DOM talks only of single documents (don't ask me why), what they are doing is talking about a node in the document versus a node floating in space.
would imply that it is possible for a newChild to be added that is not already in the tree. But apparently it can't come from another tree, as that is cloning it, really?
I don't understand that sentence. It can't come from another document as far as the DOM spec is concerned, but I don't have a problem with Zope making the logical extension to multiple documents. You clone a node when you want a copy that you can put somewhere else (in the same tree, in standard DOM, in any tree, in an extended DOM).
The more important point is that a single element cannot have two parents. You should clone it.
Yeah, but that isn't supported by XML Document yet. :)
There's your real problem. :) -- Paul Prescod - ISOGEN Consulting Engineer speaking for only himself http://itrc.uwaterloo.ca/~papresco To me programming is more than an important practical art. It is also a gigantic undertaking in the foundations of knowledge. - Grace Hopper
Paul Prescod wrote:
Martijn Faassen wrote:
This is possible, thought the provision (in the DOM spec):
Adds the node newChild to the end of the list of children of this node. If the newChild is already in the tree, it is first removed.
Right. Since the DOM talks only of single documents (don't ask me why), what they are doing is talking about a node in the document versus a node floating in space.
I see. Zope might need a 'Node floating in Space Document', then, to make things easier (especially from DTML -- from Python it's not so big a problem to manipulate nodes).
would imply that it is possible for a newChild to be added that is not already in the tree. But apparently it can't come from another tree, as that is cloning it, really?
I don't understand that sentence.
Okay, I wasn't aware that the DOM spec was only about single documents (plus any nodes floating in space). So that part about 'if the newChild is already in the tree, it is first removed' seemed to imply that newChild could come from some other places -- and my idea was another document. I didn't think of nodes floating in space as a concept. (I'm not that familiar with the DOM yet)
It can't come from another document as far as the DOM spec is concerned, but I don't have a problem with Zope making the logical extension to multiple documents.
As long as we don't conflict with any (future?) standard I suppose..
You clone a node when you want a copy that you can put somewhere else (in the same tree, in standard DOM, in any tree, in an extended DOM).
Yes, I figured that this was the case. Sometimes one wants to add a node to a document that is not yet in that document (it's the first). In that case you can't clone a node from the doc, modify it, and move it back into the tree -- what's the DOM's stance on this (as the DOM doesn't talk about multiple documents)?
The more important point is that a single element cannot have two parents. You should clone it.
Yeah, but that isn't supported by XML Document yet. :)
There's your real problem. :)
I'll be working on that problem. :) Regards, Martijn
Martijn Faassen wrote:
Yes, I figured that this was the case. Sometimes one wants to add a node to a document that is not yet in that document (it's the first). In that case you can't clone a node from the doc, modify it, and move it back into the tree -- what's the DOM's stance on this (as the DOM doesn't talk about multiple documents)?
There are methods called CreateElement, CreateAttribute, CreateProcessingInstruction, etc. Paul Prescod
On Mon, 23 Aug 1999, Paul Prescod wrote:
Martijn Faassen wrote:
Yes, I figured that this was the case. Sometimes one wants to add a node to a document that is not yet in that document (it's the first). In that case you can't clone a node from the doc, modify it, and move it back into the tree -- what's the DOM's stance on this (as the DOM doesn't talk about multiple documents)?
There are methods called CreateElement, CreateAttribute, CreateProcessingInstruction, etc.
Have I missed something profound here? I thought the current Zope XML DOM was read-only!
Paul Prescod
Cheers, Anthony Pfrunder
Anthony Pfrunder wrote:
Have I missed something profound here? I thought the current Zope XML DOM was read-only!
I was answering a question from long ago about what the standard DOM says about creating nodes. Paul Prescod
participants (4)
-
Amos Latteier -
Anthony Pfrunder -
Martijn Faassen -
Paul Prescod