user1787633
user1787633

Reputation: 47

use commet to some tags in a xml file

I read now a xml file, I did particular tag comment but It omitted and then insert comment...

XML:

<book>
  <book-meta>
    <book-id pub-id-type="doi">1545</book-id>
</book-meta>
</book>

script:

 use strict;
use XML::Twig;
open(my $out, '>', 'Output.xml') or die "can't Create stroy file $!\n";

my $story_file = XML::Twig->new(
                               twig_handlers =>{
                                        'book-id' => sub {$_->set_comment('drop')},
                                        keep_atts_order => 1,
                                        },
                                        pretty_print => 'indented',
                              );

$story_file->parsefile('sample.xml');
$story_file->print($out);

my output is:

<book>
  <book-meta><!--drop-->
  </book-meta>
</book>

i need out as:

<book>
  <book-meta>
    <!--<book-id pub-id-type="doi">1545</book-id>-->
</book-meta>
</book>

what I wrong in this process....

Upvotes: 0

Views: 51

Answers (1)

choroba
choroba

Reputation: 241988

Just set the comment text to the XML of the twig:

'book-id' => sub { $_->set_comment($_->sprint) },

Upvotes: 1

Related Questions