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

When I am trying to output an xml declaration line, I am getting error message "xml declaration line should be the first line"

This happens when I am working on Interwoven TeamSite

If I use "\" in the code, will it take away the meaning of xml declaration and print xml declaration literally ?

Following is the code that I am using. iw_ostream outputs a separate file. So first <?xml version="1.0" encoding="UTF-8"?> is output in one file, while <?xml version="1.0" encoding="UTF-8"?> under iw_ostream is output in dates.xml file. Its the second xml declaration line that causes problem.

I was advised to use <iw_perl>iwpt_output(qq(\<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n));</iw_perl>, but that did not help either.
<?xml version="1.0" encoding="UTF-8"?> <iw_pt name="content"><iw_perl><![CDATA[ $xmlFileName = "xml/dates.xml"; $CDATA_open='<!'.'[CDATA['; $CDATA_close=']]'.'>'; ]]> </iw_perl> <iw_ostream file_val="xmlFileName"> <?xml version="1.0" encoding="UTF-8"?> <eventslist> <events> <iw_iterate var='event1' list='dcr.event.EventContentContainer' it +eration='count'> <event id="{iw_value name='event1.id'}"> <active><iw_value name="$CDATA_open"/><iw_value name='event1.a +ctive'/><iw_value name="$CDATA_close"/></active> <eventdate><iw_value name="$CDATA_open"/><iw_value name='event +1.eventDate'/><iw_value name="$CDATA_close"/></eventdate> - - - -

Replies are listed 'Best First'.
Re: printing xml declaration
by kcott (Archbishop) on Aug 30, 2011 at 12:49 UTC

    I would check that whatever text is output (for the XML declaration) actually appears on the first line. It's quite possible you have an extraneous newline before this declaration (which may not be immediately obvious from a brief, visual inspection).

    For your string, I'd suggest using single-quoting (q), rather than double-quoting (qq), and eliminate all the backslashes:

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

    You can still embed newlines in a single-quoted string:

    my $three_lines = q{Line 1 Line 2 Line 3};

    Here's a real-life example taken from code I use daily:

    q{<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet ...

    I have no idea what Interwoven TeamSite is - if important to your question, please provide an appropriate link. If you have follow-up questions, please include all relevant code, input and output: paraphrased messages and prosaic code descriptions are far less useful.

    -- Ken

Re: printing xml declaration
by Anonymous Monk on Aug 30, 2011 at 12:01 UTC

    If I use "\" in the code, will it take away the meaning of xml declaration and print xml declaration literally ?

    That depends entirely on exactly what you mean. See How do I post a question effectively?