如何更改XML以使用空元素标签?

时间:2021-01-21 12:16:00

I am new to XML::Twig. How can I change all empty elements to use empty-element tags (<foo/>) instead of a start-tag and end-tag combo (<foo></foo>)?

我是XML :: Twig的新手。如何更改所有空元素以使用空元素标记( )而不是start-tag和end-tag组合( )?

Input:

输入:

<book>
    <given-names>Maurice<xref ref-type="fn" rid="fnI_1"></xref></given-names>
    <colspec colname="col1" colnum="1"></colspec>
    <entry align="left"><p></p></entry>
</book>

I need output as:

我需要输出为:

<book>
    <given-names>Maurice<xref ref-type="fn" rid="fnI_1"/></given-names>
    <colspec colname="col1" colnum="1"/>
    <entry align="left"><p/></entry>
</book>

I tried:

我试过了:

       use XML::Twig;
       my $xml = XML::Twig->new(twig_handlers => {
                                  'xref' => sub {$_->set_tag('#EMPTY'),},
                                },
                                pretty_print => 'indented',                                        
                               );
       $xml->parse('sample.xml');
       $xml->print;
}

But I can't process it. How can change gloabally without content tag to empty tag? how can I change?

但我无法处理它。如何在没有内容标签的情况下全面改变空标签?我该怎么改变?

2 个解决方案

#1


2  

If you want to stick with Twig, you can do it like this:

如果你想坚持使用Twig,你可以这样做:

#!usr/bin/perl
use strict;
use warnings;
use XML::Twig;

my $xml = XML::Twig->new(twig_handlers => {
             'p' => sub { 
                 if (!$_->first_child()) { $_->set_content('#EMPTY') } 
              },
           },
           pretty_print => 'indented',
           empty_tags => 'normal'                                 
);

$xml->parsefile('file.xml');
$xml->print;

Basically you have to manually check if the element contains nothing, then set it to be an empty element.

基本上你必须手动检查元素是否包含任何内容,然后将其设置为空元素。

#2


5  

XML::LibXML will automatically output the shorter version.

XML :: LibXML将自动输出较短的版本。

use XML::LibXML qw( );
print XML::LibXML->new()->parse_file($ARGV[0])->toString();

As for XML::Twig, it also uses the shorter form by default (empty_tags => 'normal'). However, it only considers empty elements those that were created from <foo/>. (Seems pretty stupid to me!) I did some digging and found that it does allow you change if it considers an element empty or not. This is done using set_empty and set_not_empty.

对于XML :: Twig,默认情况下它也使用较短的形式(empty_tags =>'normal')。但是,它只考虑从 创建的空元素。 (对我来说似乎很蠢!)我做了一些挖掘,发现如果它认为元素是空的,它确实允许你改变。这是使用set_empty和set_not_empty完成的。

use XML::Twig qw( );
my $twig = XML::Twig->new(
   twig_handlers => {
      '*' => sub {
         $_->set_empty() if !$_->first_child();
      },
   },
);
$twig->parsefile($ARGV[0]);
$twig->print();

#1


2  

If you want to stick with Twig, you can do it like this:

如果你想坚持使用Twig,你可以这样做:

#!usr/bin/perl
use strict;
use warnings;
use XML::Twig;

my $xml = XML::Twig->new(twig_handlers => {
             'p' => sub { 
                 if (!$_->first_child()) { $_->set_content('#EMPTY') } 
              },
           },
           pretty_print => 'indented',
           empty_tags => 'normal'                                 
);

$xml->parsefile('file.xml');
$xml->print;

Basically you have to manually check if the element contains nothing, then set it to be an empty element.

基本上你必须手动检查元素是否包含任何内容,然后将其设置为空元素。

#2


5  

XML::LibXML will automatically output the shorter version.

XML :: LibXML将自动输出较短的版本。

use XML::LibXML qw( );
print XML::LibXML->new()->parse_file($ARGV[0])->toString();

As for XML::Twig, it also uses the shorter form by default (empty_tags => 'normal'). However, it only considers empty elements those that were created from <foo/>. (Seems pretty stupid to me!) I did some digging and found that it does allow you change if it considers an element empty or not. This is done using set_empty and set_not_empty.

对于XML :: Twig,默认情况下它也使用较短的形式(empty_tags =>'normal')。但是,它只考虑从 创建的空元素。 (对我来说似乎很蠢!)我做了一些挖掘,发现如果它认为元素是空的,它确实允许你改变。这是使用set_empty和set_not_empty完成的。

use XML::Twig qw( );
my $twig = XML::Twig->new(
   twig_handlers => {
      '*' => sub {
         $_->set_empty() if !$_->first_child();
      },
   },
);
$twig->parsefile($ARGV[0]);
$twig->print();