RemoveNodes function - accepts XPath

Discussion in 'XML' started by Hoss, Dec 27, 2006.

  1. Hoss

    Hoss Guest

    Hello.

    Because IE and Mozilla have such completely different XML
    implementations, I have created a class to handle general XML tasks,
    such as iterating over nodes given an xpath, evaluating an xpath, ect.
    It does all the branching for the different implementations within
    itself. I am working on a new method for this class that will, given an
    xpath, remove all nodes that match from the document. It works great in
    IE, heres the IE code.

    var nodeList = this.xdoc.selectNodes(xpath);
    for(var x = 0; x < nodeList.length; x++)
    {
    nodeList[x].parentNode.removeChild(nodeList[x]);
    }

    However, Mozilla has a different way of doing things, and to evaluate
    an xpath you have to specify if you want an iterator result, a snapshot
    result, or a firstnode result type. Heres the problem:

    If I return an iterator results type from the xpath, and then iterate
    over the results, the iterator becomes invalid once I use the iterator
    to modify the document tree . . . so that wont work.

    If I return a snapshopt type from the xpath, and then loop over that,
    each item is now disconnected from the document tree and modifying them
    doesnt affect the document tree at all. So that doesnt work either.

    Surely someone has tried this before ??? Heres my best efforts in
    Firefox

    var iterator = this.xdoc.evaluate(xpath, this.xdoc, null,
    XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
    var theNode = iterator.iterateNext();
    while(theNode)
    {
    theNode.parentNode.removeChild(theNode);
    theNode = iterator.iterateNext();
    }
     
    Hoss, Dec 27, 2006
    #1
    1. Advertising

  2. * Hoss wrote in comp.text.xml:
    >If I return a snapshopt type from the xpath, and then loop over that,
    >each item is now disconnected from the document tree and modifying them
    >doesnt affect the document tree at all. So that doesnt work either.


    This sounds like a bug. What you can do then is to use an iterator,
    collect all the nodes into a separate Array, and once iteration is
    complete, iterate over the array and remove each node as you go. As
    you modify the document only after you no longer need the iterator,
    invalidating the iterator would be no problem for your application.
    --
    Björn Höhrmann · mailto: · http://bjoern.hoehrmann.de
    Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
    68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
     
    Bjoern Hoehrmann, Dec 27, 2006
    #2
    1. Advertising

  3. Hoss

    Hoss Guest

    Gosh .... how is it possible that I didnt think of that !? Thanks
    Bjoern, works great.

    On Dec 27, 12:37 pm, Bjoern Hoehrmann <> wrote:
    > * Hoss wrote in comp.text.xml:
    >
    > >If I return a snapshopt type from the xpath, and then loop over that,
    > >each item is now disconnected from the document tree and modifying them
    > >doesnt affect the document tree at all. So that doesnt work either.This sounds like a bug. What you can do then is to use an iterator,

    > collect all the nodes into a separate Array, and once iteration is
    > complete, iterate over the array and remove each node as you go. As
    > you modify the document only after you no longer need the iterator,
    > invalidating the iterator would be no problem for your application.
    > --
    > Björn Höhrmann · mailto: ·http://bjoern.hoehrmann.de
    > Weinh. Str. 22 · Telefon: +49(0)621/4309674 ·http://www.bjoernsworld.de
    > 68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 ·http://www.websitedev.de/
     
    Hoss, Dec 27, 2006
    #3
  4. Hoss wrote:

    > If I return a snapshopt type from the xpath, and then loop over that,
    > each item is now disconnected from the document tree and modifying them
    > doesnt affect the document tree at all. So that doesnt work either.


    Is that your reading of the W3C DOM Level 3 XPath note or your
    experience with Mozilla?

    Obviously if you want to remove nodes then they are disconnected once
    you remove them. But if you remove one node from the list the snapshot
    gives you then you can still work with the other nodes in the snapshot
    to remove them.
    Simple example is here:
    <http://home.arcor.de/martin.honnen/xslt/test2006122801.html>
    Works fine for me with Mozilla (tested with Gecko 1.7, 1.8.0, 1.8.1) and
    with Opera 9.



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 28, 2006
    #4
  5. Hoss

    Hoss Guest

    Yeah, this is from practical experience with Mozilla. And really, the
    whole point of a snapshot is to have a nodelist that isnt connected to
    the document tree from whence it came. That way if you wanted to take 5
    minutes going through all the nodes your guaranteed that they will
    exist in the snapshot the way that they existed in the document tree at
    the time you ran your xpath. With an iterator, during that 5 minute
    time-frame, some other process just might have mutated the document
    tree; so of course your iterator has to then be invalidated.

    > Obviously if you want to remove nodes then they are disconnected once
    > you remove them. But if you remove one node from the list the snapshot
    > gives you then you can still work with the other nodes in the snapshot
    > to remove them.
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/


    What I am saying is that once you SNAPSHOT - poof - the thing you have
    is already a separate entity from the original document tree. That
    means that if you discombobulate every node in the snapshot, the
    original document tree remains intact. Thats the point of a snapshot,
    but it also means you cant use one to modify a DOM, which is what I was
    trying to do.
     
    Hoss, Dec 28, 2006
    #5
  6. Hoss wrote:

    > What I am saying is that once you SNAPSHOT - poof - the thing you have
    > is already a separate entity from the original document tree. That
    > means that if you discombobulate every node in the snapshot, the
    > original document tree remains intact. Thats the point of a snapshot,
    > but it also means you cant use one to modify a DOM, which is what I was
    > trying to do.


    Why does that test case
    <http://home.arcor.de/martin.honnen/xslt/test2006122801.html>
    then remove the nodes from the original document tree?

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 28, 2006
    #6
  7. Also see the discussion of nodelist's quirks at at
    http://www.w3.org/DOM/faq.html. (My personal opinion has always been
    that nodelist was a design mistake, but I'm told there was a specific
    member of the DOM Level 1 working group who insisted that their
    customers would prefer a broken array-like behavior to learning about
    tree behavior. The Traversal functions in DOM Level 2 are somewhat more
    robust.)

    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Jan 7, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. winbatch
    Replies:
    5
    Views:
    588
    Winbatch
    Feb 15, 2005
  2. titancipher
    Replies:
    4
    Views:
    522
    titancipher
    Jun 13, 2005
  3. u.int.32.t
    Replies:
    11
    Views:
    601
    Ian Collins
    Apr 24, 2007
  4. globalrev
    Replies:
    14
    Views:
    374
    Bruno Desthuilliers
    Apr 28, 2008
  5. Peng Yu
    Replies:
    12
    Views:
    737
    kwikius
    Sep 7, 2008
Loading...

Share This Page