gloomrider has asked for the wisdom of the Perl Monks concerning the following question:

This:
print $gen->rss([itunes => "http://www.itunes.com/dtds/podcast-1.0.d +td"], {version => '2.0'});
Produces this:
<itunes:rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" +version="2.0" />
But I want this:
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version +="2.0">
Any ideas? Thanks in advance

Replies are listed 'Best First'.
Re: XML::Generator and namespace
by dthor (Novice) on Aug 09, 2012 at 18:59 UTC

    Have you tried using the namespace option?

    my $gen = XML::Generator->new( pretty => 2, namespace => [itunes => "http://www.itunes.com/dtds/p +odcast-1.0.dtd"]);
      Thanks for the reply. I tried your suggestion:
      #!/usr/bin/perl -w use strict; use XML::Generator; my $gen = XML::Generator->new( conformance => 'strict', escape => 'always', pretty => 2, encoding => 'UTF-8', version => '1.0', namespace => [itunes => "http://www.itunes.com/dtds/podcast-1.0. +dtd"], ); print $gen->xmldecl('standalone' , undef); print $gen->rss([itunes => "http://www.itunes.com/dtds/podcast-1.0. +dtd"], {version => '2.0'});
      ...but I still get the same thing:
      <?xml version="1.0" encoding="UTF-8"?> <itunes:rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" +version="2.0" />
        In your example, you don't want the <rss> tag to be in the itunes: namespace, so you shouldn't be passing in a namespace to the corresponding XML::Generator call. All you need to do to get the xmlns:itunes attribute into the <rss> tag is to use that namespace in some embedded content. For example:
        $itunes = XML::Generator->new(namespace=>[itunes=>'http://...']); $gen = XML::Generator->new(); print $gen->rss($itunes->author("Some Guy"))
        This generates:
        <rss xmlns:itunes="http://..."><itunes:author>Some Guy</itunes></rss>
Re: XML::Generator and namespace
by influx (Beadle) on Aug 10, 2012 at 12:26 UTC

    While not the best solution, in the meantime perhaps you could wrap it in a function that returns the information you want

    use strict; use XML::Generator; sub get_ns { my $in = shift; my $ns = substr($in, 1, index($in, ":")); $in =~ s/<$ns/</g; return $in; } my $gen = XML::Generator->new( conformance => 'strict', escape => 'always', pretty => 2, encoding => 'UTF-8', version => '1.0', namespace => [itunes => "http://www.itunes.com/dtds/podcast-1.0. +dtd"], ); print $gen->xmldecl('standalone' , undef); print get_ns($gen->rss([itunes => "http://www.itunes.com/dtds/podcast- +1.0.dtd"], {version => '2.0'})) . "\n";