Subject: | Support for multiple categories |
Greetings,
the RSS 2.0 specification allows multiple <category> tags inside <item>. XML::RSS::LibXML currently reads the first only. I'm sending you a little patch to handle multiple categories. The 'category' element in the hash will be a scalar (just like now) if there's one category only, otherwise it will be an arrayref with all the values inside.
I chose to follow your liberal approach, so this behaviour will be adopted just for every multiple element that may occur into the <item> tag being parsed.
I strongly invite you to merge this patch in order to better comply with the RSS 2.0 specs. Cheers,
Alessandro Ranellucci
aar@cpan.org
--- XML-RSS-LibXML-0.14.1/lib/XML/RSS/LibXML/Parser.pm Sun Nov 20 13:09:09 2005
+++ XML-RSS-LibXML-0.14/lib/XML/RSS/LibXML/Parser.pm Tue Dec 27 15:40:47 2005
@@ -221,11 +221,12 @@
# now, for each node that we can cover, go and parse
foreach my $node ($xc->findnodes($xpath, $root)) {
+ my $val;
if ($xc->findnodes('./*', $node)) {
-# print STDERR "Parsing ", $node->getName(), " (recurse)\n";
- $sub{$node->localname} = $self->_parse_children($version, $node);
+ # print STDERR "Parsing ", $node->getName(), " (recurse)\n";
+ $val = $self->_parse_children($version, $node);
} else {
-# print STDERR "Parsing ", $node->getName(), "\n";
+ # print STDERR "Parsing ", $node->getName(), "\n";
my $text = $node->textContent();
if ($text !~ /\S/) {
$text = '';
@@ -233,14 +234,24 @@
# argh. it has attributes. we do our little hack...
if ($node->hasAttributes) {
- $sub{$node->localname} = XML::RSS::LibXML::MagicElement->new(
+ $val = XML::RSS::LibXML::MagicElement->new(
content => $text,
attributes => [ $node->attributes ]
);
} else {
- $sub{$node->localname} = $text;
+ $val = $text;
}
}
+
+ # multiple values for the same key will
+ # be stored as an arrayref instead of a scalar
+ if (!defined $sub{$node->localname}) {
+ $sub{$node->localname} = $val;
+ } elsif (ref $sub{$node->localname} eq 'ARRAY') {
+ push @{ $sub{$node->localname} }, $val;
+ } else {
+ $sub{$node->localname} = [ $sub{$node->localname}, $val ];
+ }
}
if (keys %sub) {