Subject: | Patch to create RDF:Bag elements when passed array references |
We had a number of cases where we wanted to keep RDF data in Bag elements, but XML::RSS didn't support this, and if we tried to pass them as
thing => "
<RDF:Bag>
<RDF:li>foo</RDF:li>
<RDF:li>bar</RDF:li>
</RDF:Bag>",
everything got escaped, as it should, but that was not what we wanted.
So this patch changes the encode subroutine such that if you pass it an array reference, you get a <$prefix:Bag> element with the contents of the array. E.g., if we have an array @categories with a number of categories for a news item, we add the item like so:
$rss->add_item(
title => "patch to create RDF:Bag elements when passed array references",
link => "http://rt.cpan.org",
description => "patch",
mrNews => {
categories => \@categories,
}
);
And this would output:
<mrNews:categories>
<mrNews:Bag>
<mrNews:li>foo</mrNews:li>
<mrNews:li>bar</mrNews:li>
</mrNews:Bag>
</mrNews:categories>
I suppose the optimal behavior would be to put arrays in <$prefix:Seq> and put hashes or lists in <$prefix:Bag>, but I just thought of that now, and it's not something I need.
--- RSS-bag.pm 2005-03-29 12:37:19.000000000 -0500
+++ XML-RSS-1.05/lib/RSS.pm 2005-03-29 11:05:28.000000000 -0500
@@ -6,7 +6,7 @@
use XML::Parser;
use vars qw($VERSION $AUTOLOAD @ISA $modules $AUTO_ADD);
-$VERSION = '1.05-bag';
+$VERSION = '1.05';
@ISA = qw(XML::Parser);
$AUTO_ADD = 0;
@@ -906,7 +906,7 @@
qq!" />\n!;
}
else {
- $output .= "<$prefix:$el>". $self->encode($value,$prefix) ."</$prefix:$el>\n";
+ $output .= "<$prefix:$el>". $self->encode($value) ."</$prefix:$el>\n";
}
}
}
@@ -976,7 +976,7 @@
qq!" />\n!;
}
else {
- $output .= "<$prefix:$el>". $self->encode($value,$prefix) ."</$prefix:$el>\n";
+ $output .= "<$prefix:$el>". $self->encode($value) ."</$prefix:$el>\n";
}
}
}
@@ -1022,7 +1022,7 @@
qq!" />\n!;
}
else {
- $output .= "<$prefix:$el>". $self->encode($value,$prefix) ."</$prefix:$el>\n";
+ $output .= "<$prefix:$el>". $self->encode($value) ."</$prefix:$el>\n";
}
}
}
@@ -1051,7 +1051,7 @@
while ( my($url, $prefix) = each %{$self->{modules}} ) {
next if $prefix =~ /^(dc|syn|taxo)$/;
while ( my($el, $value) = each %{$self->{textinput}->{$prefix}} ) {
- $output .= "<$prefix:$el>". $self->encode($value,$prefix) ."</$prefix:$el>\n";
+ $output .= "<$prefix:$el>". $self->encode($value) ."</$prefix:$el>\n";
}
}
@@ -1781,31 +1781,17 @@
my $entities = join('|', keys %entity);
sub encode {
- my ($self, $text, $prefix) = @_;
- return $text unless $self->{'encode_output'};
-
- my $encoded_text = '';
- if (ref $text eq 'ARRAY') {
- if(!$prefix) { $prefix = "rdf"; }
- $encoded_text = "\n <$prefix:Bag>";
- foreach my $bagitem (@{$text}) {
- $encoded_text .= "\n <$prefix:li>";
- while ( $bagitem =~ s/(.*?)(\<\!\[CDATA\[.*?\]\]\>)//s ) {
- $encoded_text .= encode_text($1) . $2;
- }
- $encoded_text .= encode_text($bagitem);
- $encoded_text .= "</$prefix:li>";
- }
- $encoded_text .= "\n </$prefix:Bag>\n";
- }
-
- else {
+ my ($self, $text) = @_;
+ return $text unless $self->{'encode_output'};
+
+ my $encoded_text = '';
+
while ( $text =~ s/(.*?)(\<\!\[CDATA\[.*?\]\]\>)//s ) {
- $encoded_text .= encode_text($1) . $2;
+ $encoded_text .= encode_text($1) . $2;
}
$encoded_text .= encode_text($text);
- }
- return $encoded_text;
+
+ return $encoded_text;
}
sub encode_text {