我有一个带有重复节点的站点地图视频文件xml:
<?xml version="1.0"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
<url>
<loc>http://www.tubtun.com/video/Samsung_42Channel_Wireless_SoundStand</loc>
<video:video>
<video:title>Samsung 42Channel Wireless SoundStand</video:title>
<video:description>Samsung 4.2Channel Wireless SoundStand</video:description>
<video:thumbnail_loc>http://www.tubtun.com/media/files_thumbnail/user91/pl_5364844b0dc.jpg</video:thumbnail_loc>
<video:player_loc>http://www.tubtun.com/modules/vPlayer/vPlayer.swf?f=http://www.tubtun.com/modules/vPlayer/vPlayercfg.php?fid=844b0dc2c7258f4de11</video:player_loc>
<video:publication_date>2015-01-27</video:publication_date>
</video:video>
</url>
<url>
<loc>http://www.tubtun.com/video/Samsung_42Channel_Wireless_SoundStand</loc>
<video:video>
<video:title>Samsung 42Channel Wireless SoundStand</video:title>
<video:description>Samsung 4.2Channel Wireless SoundStand</video:description>
<video:thumbnail_loc>http://www.tubtun.com/media/files_thumbnail/user91/pl_5364844b0dc.jpg</video:thumbnail_loc>
<video:player_loc>http://www.tubtun.com/modules/vPlayer/vPlayer.swf?f=http://www.tubtun.com/modules/vPlayer/vPlayercfg.php?fid=844b0dc2c7258f4de11</video:player_loc>
<video:publication_date>2015-01-27</video:publication_date>
</video:video>
</url>
.....
我编写了一个perl脚本来删除这些重复的数据:
use strict;
use warnings;
use XML::LibXML;
my $file = 'sitemap.xml';
my $doc = XML::LibXML->load_xml( location => $file );
my %seen;
foreach my $uni ( $doc->findnodes('//url') ) { # 'university' nodes only
my $name = $uni->find('video:title');
print "'$name' duplicated\n",
$uni->unbindNode() if $seen{$name}++; # Remove if seen before
}
$doc->toFile('clarified.xml'); # Print to file
不幸的是,文件&#34; clarified.xml&#34;与sitemap.xml相同。
我不知道我的剧本有什么问题。
答案 0 :(得分:1)
您应该使用XPathContext
并注册视频和默认名称空间。您还应该调用findvalue
来获取标题作为字符串。
my $xpc = XML::LibXML::XPathContext->new();
$xpc->registerNs(sitemap => 'http://www.sitemaps.org/schemas/sitemap/0.9');
$xpc->registerNs(video => 'http://www.google.com/schemas/sitemap-video/1.1');
for my $node ($xpc->findnodes('//sitemap:url', $doc)) {
my $name = $xpc->findvalue('video:title', $node);
...
}
答案 1 :(得分:0)
我不太确定为什么XML::LibXML
无法正常工作,尽管如评论中所述 - 如果它不与find
合作,那么&#39 ;是它的根源。
我会提供一种使用XML::Twig
工作的替代方法。
#!/usr/bin/env perl
use strict;
use warnings;
use XML::Twig;
my $file = 'test3.xml';
my %seen;
sub delete_url_if_seen {
my ( $twig, $url ) = @_;
my $name = $url -> get_xpath('./video:video/video:title',0) -> trimmed_text;
if ( $seen{$name}++ ) { $url -> delete; };
}
my $twig = XML::Twig -> new ( 'pretty_print' => 'indented_a',
'twig_handlers' => { 'url' => \&delete_url_if_seen } );
$twig -> parsefile_inplace ( $file );
答案 2 :(得分:0)
我有它工作,这里是代码&amp;我尝试了https://stackoverflow.com/a/4817929/235961
中提供的解决方案use strict;
use warnings;
use XML::LibXML;
my $file = 'sitemap.xml';
my $doc = XML::LibXML->load_xml( location => $file );
my %seen;
foreach my $uni ( $doc->findnodes("//*[name() ='url']") ) { # 'university' nodes only
my $name = $uni->find('//video:title');
print "'$name' duplicated\n",
$uni->unbindNode() if $seen{$name}++; # Remove if seen before
}
$doc->toFile('clarified.xml'); # Print to file