Search code examples
perlxml-libxml

Does XML::LibXML::Node replaceNode() replace Perl instances of the replaced node


... so after I replace a with b, all previous references to a now point to b?


Solution

  • #!/usr/bin/perl
    use warnings;
    use strict;
    
    use XML::LibXML;
    
    my $dom = 'XML::LibXML'->load_xml(string => '<r><p><c/></p></r>');
    my ($n1) = $dom->findnodes('/r/p/c');
    my ($n2) = $dom->findnodes('/r/p/c');
    $n1->replaceNode('XML::LibXML::Element'->new('n'));
    print $dom;
    print $n1, $n2;
    

    Output:

    <?xml version="1.0"?>
    <r><p><n/></p></r>
    <c/><c/>