=== t/xml-header.t
==================================================================
--- t/xml-header.t (revision 445)
+++ t/xml-header.t (revision 448)
@@ -0,0 +1,176 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 8;
+
+use XML::RSS;
+
+sub starts_with
+{
+ local $Test::Builder::Level = $Test::Builder::Level + 1;
+ my ($rss, $prefix, $msg) = @_;
+ my $rss_output = $rss->as_string();
+ my $ok = is (
+ substr($rss_output, 0, length($prefix)),
+ $prefix,
+ $msg
+ );
+}
+
+sub create_rss_1
+{
+ my $args = shift;
+ # my $rss = new XML::RSS (version => '0.9');
+ my @style =
+ exists($args->{stylesheet}) ?
+ (stylesheet => $args->{stylesheet}) :
+ ()
+ ;
+ my $rss = XML::RSS->new(
+ version => $args->{version},
+ @style
+ );
+ my $image_link = exists($args->{image_link}) ? $args->{image_link} :
+ "
http://freshmeat.net/";
+
+ my $extra_image_params = $args->{image_params} || [];
+
+ $rss->channel(
+ title => "freshmeat.net",
+ link => "
http://freshmeat.net",
+ description => "the one-stop-shop for all your Linux software needs",
+ );
+
+ $rss->image(
+ title => "freshmeat.net",
+ url => "0",
+ link => $image_link,
+ @{$extra_image_params},
+ );
+
+ $rss->add_item(
+ title => "GTKeyboard 0.85",
+ link => "
http://freshmeat.net/news/1999/06/21/930003829.html"
+ );
+
+ return $rss;
+}
+
+{
+ # TEST
+ starts_with(
+ create_rss_1({'version' => "0.9"}),
+ <<'EOF',
+<?xml version="1.0" encoding="UTF-8"?>
+
+<rdf:RDF
+EOF
+ "header of RSS 0.9 without the stylesheet"
+ );
+}
+
+{
+ # TEST
+ starts_with(
+ create_rss_1({'version' => "0.91"}),
+ <<'EOF',
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN"
+ "
http://my.netscape.com/publish/formats/rss-0.91.dtd">
+
+<rss version="0.91">
+EOF
+ "header of RSS 0.9.1 without the stylesheet"
+ );
+}
+
+
+{
+ # TEST
+ starts_with(
+ create_rss_1({'version' => "1.0"}),
+ <<'EOF',
+<?xml version="1.0" encoding="UTF-8"?>
+
+<rdf:RDF
+EOF
+ "header of RSS 1.0 without the stylesheet"
+ );
+}
+
+{
+ # TEST
+ starts_with(
+ create_rss_1({'version' => "2.0"}),
+ <<'EOF',
+<?xml version="1.0" encoding="UTF-8"?>
+
+<rss
+ version="2.0"
+EOF
+ "header of RSS 2.0 without the stylesheet"
+ );
+}
+
+{
+ # TEST
+ starts_with(
+ create_rss_1({'version' => "0.9", stylesheet => "
http://myhost.tld/foo.xsl"}),
+ <<'EOF',
+<?xml version="1.0" encoding="UTF-8"?>
+<?xml-stylesheet type="text/xsl" href="http://myhost.tld/foo.xsl"?>
+
+<rdf:RDF
+EOF
+ "header of RSS 0.9 with the stylesheet"
+ );
+}
+
+{
+ # TEST
+ starts_with(
+ create_rss_1({'version' => "0.91", stylesheet => "
http://myhost.tld/foo.xsl"}),
+ <<'EOF',
+<?xml version="1.0" encoding="UTF-8"?>
+<?xml-stylesheet type="text/xsl" href="http://myhost.tld/foo.xsl"?>
+
+<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN"
+ "
http://my.netscape.com/publish/formats/rss-0.91.dtd">
+
+<rss version="0.91">
+EOF
+ "header of RSS 0.9.1 with the stylesheet"
+ );
+}
+
+{
+ # TEST
+ starts_with(
+ create_rss_1({'version' => "1.0", stylesheet => "
http://myhost.tld/foo.xsl"}),
+ <<'EOF',
+<?xml version="1.0" encoding="UTF-8"?>
+<?xml-stylesheet type="text/xsl" href="http://myhost.tld/foo.xsl"?>
+
+<rdf:RDF
+EOF
+ "header of RSS 1.0 without the stylesheet"
+ );
+}
+
+{
+ # TEST
+ starts_with(
+ create_rss_1({'version' => "2.0", stylesheet => "
http://myhost.tld/foo.xsl"}),
+ <<'EOF',
+<?xml version="1.0" encoding="UTF-8"?>
+<?xml-stylesheet type="text/xsl" href="http://myhost.tld/foo.xsl"?>
+
+<rss
+ version="2.0"
+EOF
+ "header of RSS 2.0 without the stylesheet"
+ );
+}
=== t/test_manifest
==================================================================
--- t/test_manifest (revision 445)
+++ t/test_manifest (revision 448)
@@ -24,3 +24,4 @@
#pod-coverage.t
rss2-gt-encoding.t
charset1.t
+xml-header.t
=== MANIFEST
==================================================================
--- MANIFEST (revision 445)
+++ MANIFEST (revision 448)
@@ -53,4 +53,5 @@
t/rss2-gt-encoding.t
t/test_manifest
t/version.t
+t/xml-header.t
TODO
=== lib/XML/RSS.pm
==================================================================
--- lib/XML/RSS.pm (revision 445)
+++ lib/XML/RSS.pm (revision 448)
@@ -496,6 +496,12 @@
? ($self->{encoding} = $hash{encoding})
: ($self->{encoding} = 'UTF-8');
+ # stylesheet
+ if (exists($hash{stylesheet}))
+ {
+ $self->{stylesheet} = $hash{stylesheet};
+ }
+
# initialize RSS data structure
# RSS version 0.9
if ($self->{version} eq '0.9') {
@@ -740,15 +746,29 @@
}
}
+sub _output_xml_declaration
+{
+ my $self = shift;
+
+ $self->_out('<?xml version="1.0" encoding="'.$self->{encoding}.'"?>'."\n");
+ if (defined($self->{stylesheet}))
+ {
+ my $style_url = $self->_encode($self->{stylesheet});
+ $self->_out(qq{<?xml-stylesheet type="text/xsl" href="$style_url"?>\n});
+ }
+
+ $self->_out("\n");
+
+ return undef;
+}
+
sub as_rss_0_9 {
my $self = shift;
my $output;
$self->_set_output_var(\$output);
- # XML declaration
- my $encoding = exists $$self{encoding} ? qq| encoding="$$self{encoding}"| : '';
- $output .= qq|<?xml version="1.0"$encoding?>\n\n|;
+ $self->_output_xml_declaration();
# RDF root element
$output .= '<rdf:RDF'."\n".'xmlns:rdf="
http://www.w3.org/1999/02/22-rdf-syntax-ns#"'."\n";
@@ -869,9 +889,9 @@
my $output;
$self->_set_output_var(\$output);
- # XML declaration
- $output .= '<?xml version="1.0" encoding="'.$self->{encoding}.'"?>'."\n\n";
+ $self->_output_xml_declaration();
+
# DOCTYPE
$output .= '<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN"'."\n";
$output .= ' "
http://my.netscape.com/publish/formats/rss-0.91.dtd">'."\n\n";
@@ -996,8 +1016,7 @@
$self->_set_output_var(\$output);
- # XML declaration
- $output .= '<?xml version="1.0" encoding="'.$self->{encoding}.'"?>'."\n\n";
+ $self->_output_xml_declaration();
# RDF namespaces declaration
$output .="<rdf:RDF"."\n";
@@ -1278,9 +1297,9 @@
my $output;
$self->_set_output_var(\$output);
- # XML declaration
- $output .= '<?xml version="1.0" encoding="'.$self->{encoding}.'"?>'."\n\n";
+ $self->_output_xml_declaration();
+
# DOCTYPE
# $output .= '<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN"'."\n";
# $output .= ' "
http://my.netscape.com/publish/formats/rss-0.91.dtd">'."\n\n";
@@ -2126,7 +2145,7 @@
=over 4
=item new XML::RSS (version=>$version, encoding=>$encoding,
-output=>$output)
+output=>$output, stylesheet=>$stylesheet_url)
Constructor for XML::RSS. It returns a reference to an XML::RSS object.
You may also pass the RSS version and the XML encoding to use. The default
@@ -2134,8 +2153,13 @@
the B<output> format regarless of the input version. This comes in handy
when you want to convert RSS between versions. The XML::RSS modules
will convert between any of the formats. If you set <encode_output> XML::RSS
-will make sure to encode any entities in generated RSS. This is now on by default.
+will make sure to encode any entities in generated RSS. This is now on by
+default.
+You can also pass an optional URL to an XSL stylesheet that can be used to
+output an C<<< <?xsl-stylesheet ... ?> >>> meta-tag in the header that will
+allow some browsers to render the RSS file as HTML.
+
=item add_item (title=>$title, link=>$link, description=>$desc, mode=>$mode)
Adds an item to the XML::RSS object. B<mode> and B<description> are optional.