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

Greetings! It is Friday and my brain is more Fri than day at the moment. I am trying to get XML::Generator to do what i want -- produce an XML declaration that looks like this:

<?xml version="1.0" encoding="UTF-8"?>

Here is my code:

use XML::Generator; my $gen = XML::Generator->new( conformance => 'strict', version => '1.0', encoding => 'UTF-8', standalone => undef, ); print $gen->xml( $gen->foo() );

As you can see, i am explicitly setting standalone to undef which, according to the docs, is suppose to prevent the attribute from being printed in the declaration. However, this does not work for me -- this is what i get:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

I have notice in my trial and (many) errors that if i comment out the conformance => 'strict', line, then NO attribute will be added to the declaration. It's like i'm damned if i do or damned if don't. At this point I am wondering why I don't just add the string myself. :/

If anyone can see what i am doing wrong then please let me know. Thanks in advance!

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: Getting XML::Generator to DWIW with the XML declaration
by ikegami (Patriarch) on Nov 07, 2008 at 19:43 UTC

    The docs says the constructor's encoding option "Sets the default encoding for use in XML declarations."

    The docs say "Prepends an XML declaration, and re-blesses the argument into a "final" class that can't be embedded." of the xml method.

    The docs are wrong. The XML declaration is only prepended when conformance => 'strict'

    use XML::Generator qw( ); my $gen = XML::Generator->new( conformance => 'strict', encoding => 'UTF-8', ); print $gen->xml( $gen->doc('...') );
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <doc>...</doc>

    Did you want without strict?

    Update: I just copied this from my pad. I should have made sure there wasn't anything new in the question first. I missed the bit about standalone.

      Very close ... but i changed my requirements on you per our discussion in the CB. The trick now is to remove the standalone attribute. Nonetheless, thanks for the feedback. I was starting to think 12 months of solid PHP development had ... oh who i am kidding -- it HAS made my Perl rusty! :D

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
Re: Getting XML::Generator to DWIW with the XML declaration
by ikegami (Patriarch) on Nov 07, 2008 at 19:53 UTC

    As you can see, i am explicitly setting standalone to undef which, according to the docs, is suppose to prevent the attribute from being printed in the declaration.

    I don't see anywhere in the docs that say standalone is a constructor option.

    If a dtd was set in the constructor, the standalone attribute of the declaration will be set to 'no' and the doctype declaration will be appended to the XML declartion, otherwise the standalone attribute will be set to 'yes'. This can be overridden by providing a 'standalone' key in @args. If you do not want the standalone attribute to show up, explicitly provide undef as the value.

    So just add the doctype

    use XML::Generator qw( ); my $gen = XML::Generator->new( conformance => 'strict', encoding => 'UTF-8', dtd => [ 'html', 'PUBLIC', $xhtml_w3c, $xhtml_dtd ], ); print $gen->xml( $gen->html('...') );

      Perhaps i don't understand XML then. All that I want is this:

      <?xml version="1.0" encoding="UTF-8">
      Without adding a doctype. The example that I am modeling has neither. I am merely trying to produce exactly what the specs i have in front of me specify. At this point i am very inclined to simply prepend this to my XML:
      qq|<?xml version="1.0" encoding="UTF-8" ?>\n|
      Because it works. *shrug* :/

      UPDATE: OK ... I finally see what I am doing wrong now. But, in my defense -- these docs are crappy. The solution for me is to not "wrap" the xml() method around the XML tags, but to instead concat the output of xmldecl() to my XML tags:

      $gen->xmldecl( standalone => undef ) . $gen->foo( $gen->bar() );
      RANT: why can't people understand that examples are worth a thousand words? I make a strict point to clearly show how to use my code in my documentation because the last thing i want is for people to become frustrated and confused and when using my code.

      But it works -- thanks for all your help ikegami!

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)