in reply to XML::LibXML cut and paste node

Please note that your code as submitted does not work -- there is no test1.xml. I rewrote it to use <DATA>.

The main problem is that cloneNode only makes a shallow copy; in fact you can still use the node $dead after it is unbound and it still contains all the child info. Here is my rewritten program:

#!/usr/bin/perl -w use strict; use XML::LibXML; my $parser = XML::LibXML->new(); my $doc = $parser->parse_string(join "", <DATA>); # copy the node, then delete it my $from_path = q{/category/subcategory/product[product_id = "800.55.0 +1"]}; my @from_nodes = $doc->findnodes( $from_path); my $dead = $from_nodes[0]; # Assume there can be only one $dead->unbindNode; # paste the node to a new location my $to_path = q{/category/subcategory[@name = "Grand Arabica"]}; my @to_nodes = $doc->findnodes( $to_path); my $paste = $to_nodes[0]; # Assume there can be only one my $lastnode = $paste->lastChild(); $paste->insertAfter( $dead, $lastnode); print $doc->toString([0]);

-Mark

Replies are listed 'Best First'.
Re^2: XML::LibXML cut and paste node
by Maxim (Sexton) on Oct 19, 2004 at 23:46 UTC
    From the documentation, it said it will make a copy of the node. I thought it will be useful to use this command. Anyway, thanks for your help. It will really help me.

    Regard,

    Maxim
Re^2: XML::LibXML cut and paste node
by Maxim (Sexton) on Oct 27, 2004 at 06:44 UTC
    From the example you gave me, it works perfectly but when I put a variable to indentify the correct value what I want, it gives an error "xmlXPathCompiledEval: 1 object left on the stack".
    Here an emaple using ur example I change this my $from_path = q{/category/subcategory/product[product_id = "800.55.01"]}; to this my $from_path = q{/category/subcategory/product[product_id = $prod]};. It will show the error message.

    Is a problem with the stack?

    Maxim