开发者

Why is XML::LibXML adding a child to one <book> and not the other?

开发者 https://www.devze.com 2023-03-31 14:56 出处:网络
I\'m seeing some odd behavior with XML::LibXML. The code below is intended to add <year>2005</year> to both <book> nodes. Is something wrong here? I\'ve tried changing the XPath quer

I'm seeing some odd behavior with XML::LibXML.

The code below is intended to add <year>2005</year> to both <book> nodes. Is something wrong here? I've tried changing the XPath query (//library/book ) but the result is the same.

use strict;
use warnings;
use XML::LibXML;

my $xml = XML::LibXML->new->parse_string( << 'MAIN' );
  <library>
    <book>
      <title>Perl Best Practices</title>
      <author>Damian Conway</author>
      <isbn>0596001738</isbn>
      <pages>542</pages>
      <image src="http://www.oreilly.com/catalog/covers/perlbp.s.gif"
             width="145" height="190" />
    </book>
    <book>
      <title>Perl Cookbook, Second Edition</title>
      <author>Tom Christiansen</author>
      <author>Nathan Torkington</author>
      <isbn>059600313开发者_Go百科7</isbn>
      <pages>964</pages>
      <image src="http://www.oreilly.com/catalog/covers/perlckbk2.s.gif"
             width="145" height="190" />
    </book>
  </library>
MAIN

my ( $age ) = XML::LibXML->new
                ->parse_string( '<year>2005</year>' )
                  ->findnodes( './year' );

my @books = $xml->findnodes( '//book' );

$_->addChild( $age ) for @books;

print $xml->toString;

Output

<?xml version="1.0"?>
<library>
    <book>
      <title>Perl Best Practices</title>
      <author>Damian Conway</author>
      <isbn>0596001738</isbn>
      <pages>542</pages>
      <image src="http://www.oreilly.com/catalog/covers/perlbp.s.gif" width="145" height="190"/>
    </book>
    <book>
      <title>Perl Cookbook, Second Edition</title>
      <author>Tom Christiansen</author>
      <author>Nathan Torkington</author>
      <isbn>0596003137</isbn>
      <pages>964</pages>
      <image src="http://www.oreilly.com/catalog/covers/perlckbk2.s.gif" width="145" height="190"/>
    <year>2005</year></book>
  </library>


Adding a node creates a parent-child relationship between the node and the node to which it is being added.

A node cannot have two parents, so when you add the year node to the second book, it gets removed from the first. You need to create a node for every book.

for my $book ($xml->findnodes('//book')) {
   my $year = XML::LibXML::Element->new('year');
   $year->appendTextNode('2005');
   $book->addChild($year);
}

or

my $year = XML::LibXML::Element->new('year');
$year->appendTextNode('2005');

for my $book ($xml->findnodes('//book')) {
   $book->addChild( $year->cloneNode(1) );
}
0

精彩评论

暂无评论...
验证码 换一张
取 消