You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Gary L Peskin <ga...@firstech.com> on 2000/10/29 20:35:56 UTC

Re: XPath and cloned nodes

John Gentilin wrote:
> when I try to delete target node from my original doc, I get an
> exception "Not Owner".

-How did you build your DOM document?
-Is it the Xerces DOM implementation or the internal Xalan DOM
(DTM/Stree) implementation?
-What version of Xalan are you using? 
-How are you trying to delete your target node?

Thanks,
Gary

Re: XPath and cloned nodes

Posted by John Gentilin <jo...@eyecatching.com>.
Gary,

Thank you, what I did not realize before was that removeChild() would
only remove an immediate child. I was under the impression that the
tree would be recursed until that child, grandchild, or great grandchild
was found then delete it.

Thanks again for your time, the diagram helped.

Regards
John G



Re: XPath and cloned nodes

Posted by Gary L Peskin <ga...@firstech.com>.
John Gentilin wrote:
> Where I am confused is, I am calling the removeChild method
> from the document directly and that throws an exception "DOM8,
> Not Found"
> 
> i.e.
> Doc.removeChild(n); // throws an exception
> Doc.getDocumentElement().removeChild(n); // works fine
> 
> Shouldn't the first example represent the ultimate root node ??
> Instead it does not even consider it as part of the same document.

DOM008 doesn't mean that this the node n is in a different document than
Doc.  It means than n is not a child of Doc, which it isn't.  Your DOM
tree looks like this:

  -- Document Node (aka root of the DOM tree, what you're calling Doc)
   |
   |- doc Element (aka the documentElement)
      |
      | - name Element
      |   |
      |   |- first Attribute
      |   |- last Attribute
      | - name Element
    ...

The child of Doc is doc.  The naming here is unfortunate because it's
very confusing but name is the child of doc which is the child of Doc. 
Since your "n" represents a "name" Element, you can see from the picture
that is not a child of Doc but a grandchild of Doc.

Also, don't confuse the Document Node, which is the top of the tree,
with the document Element, which is an element node and which is the
child of the Document Node.  In a well-formed XML document, there is
only one child of the Document Node and that is called the document
element which, in your case is doc.

If this is still confusing, please ask some more.

Gary

Re: XPath and cloned nodes

Posted by John Gentilin <jo...@eyecatching.com>.
Ok, I think I get it, the code

Doc.getDocumentElement() is retrieving the nth parent
of node, so it worked

node.getParent() gets the  immediate parent and that
works too.

Both methods appear to remove the node from the source
document. i.e. they do not exist if I serialize the document
after I call removeChild().

Where I am confused is, I am calling the removeChild method
from the document directly and that throws an exception "DOM8,
Not Found"

i.e.
Doc.removeChild(n); // throws an exception
Doc.getDocumentElement.removeChild(n); // works fine

Shouldn't the first example represent the ultimate root node ??
Instead it does not even consider it as part of the same document.

Thanks
John G


> John Gentilin wrote:
> >
> > Gary,
> >
> > Thank you for the sample code. My mistake was that I was
> > calling removeChild directly from the Document. I was not
> > retrieving the documentElement first. Can you explain why  ??
>
> You have to call removeChild from the parent of the child -- not just
> from the document.  In my case, I was selecting a direct child of the
> document element so I was able to take this shortcut.  If you want to
> remove the selected node from the DOM altogether, you should probably do
> something like.
>
>   node.getParent().removeChild(node);
>
> Gary


Re: XPath and cloned nodes

Posted by Gary L Peskin <ga...@firstech.com>.
John Gentilin wrote:
> 
> Gary,
> 
> Thank you for the sample code. My mistake was that I was
> calling removeChild directly from the Document. I was not
> retrieving the documentElement first. Can you explain why  ??

You have to call removeChild from the parent of the child -- not just
from the document.  In my case, I was selecting a direct child of the
document element so I was able to take this shortcut.  If you want to
remove the selected node from the DOM altogether, you should probably do
something like.

  node.getParent().removeChild(node);

Gary

Re: XPath and cloned nodes

Posted by John Gentilin <jo...@eyecatching.com>.
Gary,

Thank you for the sample code. My mistake was that I was
calling removeChild directly from the Document. I was not
retrieving the documentElement first. Can you explain why  ??

Thanks again for all your help
John G


Re: XPath and cloned nodes

Posted by Gary L Peskin <ga...@firstech.com>.
John Gentilin wrote:
> 
> Gary,
> 
> I am using the ApplyXPath sample in the Xalan 2 D01
> source tree.  The parser being used is being created from
> the DoucmentBuilder and the DocumentBuilderFactory.
> I have not changed any of the property files so I am assuming
> that the Xerces parser is being used. Xerces.jar is in my class
> path from the Xerces project and not the Xerces.jar in the
> Xalan project  but it is the latest 1.2.1 release (Xerces is also
> in my classpath first)
> 
> To delete my target node, as I walk the NodeIterator, I call
> doc.removeChild() where doc is the original object created
> from the DocumentBuilder::parse() member.

John --

I just recreated a small case from ApplyXPath.  The
NodeIterator.nextNode() returns the actual node, not some clone of the
node, at least for me.  I'm not sure why you're getting a "Not Owner"
exception.  Can you send along the exact text and stack trace, if
possible?

Also, please send your java program, xml file, and command line showing
the XPath expression.  I'm using foo.xml supplied with the example, an
XPath argument of "/doc/name" and the attached java program.

Gary

Re: XPath and cloned nodes

Posted by John Gentilin <jo...@eyecatching.com>.
Gary,

I am using the ApplyXPath sample in the Xalan 2 D01
source tree.  The parser being used is being created from
the DoucmentBuilder and the DocumentBuilderFactory.
I have not changed any of the property files so I am assuming
that the Xerces parser is being used. Xerces.jar is in my class
path from the Xerces project and not the Xerces.jar in the
Xalan project  but it is the latest 1.2.1 release (Xerces is also
in my classpath first)

To delete my target node, as I walk the NodeIterator, I call
doc.removeChild() where doc is the original object created
from the DocumentBuilder::parse() member.

Thanks
John G

Gary L Peskin wrote:

> John Gentilin wrote:
> > when I try to delete target node from my original doc, I get an
> > exception "Not Owner".
>
> -How did you build your DOM document?
> -Is it the Xerces DOM implementation or the internal Xalan DOM
> (DTM/Stree) implementation?
> -What version of Xalan are you using?
> -How are you trying to delete your target node?
>
> Thanks,
> Gary