【发布时间】:2015-10-08 11:27:39
【问题描述】:
我有一个带有重复节点的站点地图视频文件 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
不幸的是,文件“clarified.xml”与sitemap.xml相同。
我不知道我的脚本出了什么问题。
【问题讨论】:
-
您检查过
$name的内容吗?您的脚本是否说打印foo duplicated输出? -
那么你找到了问题所在。
$uni->find('video:title')无法正常工作。您可能想检查如何使用 XML::LibXML 中的命名空间前缀,以及如何从元素中获取文本节点。
标签: xml perl xml-sitemap