Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

outputting xml

by grashoper (Monk)
on May 07, 2008 at 23:20 UTC ( [id://685350]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to input a switch so that I can add content to a page on my dbdriven website, however I can't seem to get it right today here is the code, this is using a heredoc to output and using an xslt

sub mLeftIndex { my $str = ""; #Sets content to blank my $logoutLink = "<link tab=\"\" view=\"Logout\" title=\"Logout\"/ +>"; #Generates Logout link# SWITCH: { if (($Request->item("Tab")->item() eq "Support") and ($selected +="CTS")) { # Inserts leftIndex tag for document # #$xml = qq^<leftIndex>^; #sets menuitem for left hand side # $xml.=qq^ <link tab="Support" view="ClassOutlines" title="Course Outlines +"> <description>Outlines for CTMLS Courses</description> <header>0</header> </link>; ^; #closes menuitem and closing leftIndex tag here # $xml=qq^ </leftIndex> ^; #patternmatch stuff for here document.. if (!$Request->item("View")->item() && $xml =~ /(.*?)<link tab +=\"Support\" view=\"Search\" .*?<\/link>(.*)/s) { $xml = $1.$2; } $xml =~ s/(<\/leftIndex>)/$logoutLink$1/s; $str .= $xml; } # other content for support tab # if ($Request->item("Tab")->item() eq "Support") { my $xml =qq^ <leftIndex> ^; $xml .= qq^ <link tab="Support" view="Search" title="Search"> <description>Search our Help Articles</description> <header>0</header> </link> <link tab="Support" view="HowTo" title="How To Use MLXchange"> <description>Walkthroughs for common tasks using MLXchange</descri +ption> <header>0</header> </link> <link tab="Support" view="Tutorials" title="MLXchange Tutorials"> <description>Tutorials and Demos showing you how to use MLXchange< +/description> <header>0</header> </link>^ if ( $Request->ServerVariables("SERVER_NAME")->item() =~ /m +lxhelp/i ); $xml .= qq^ <link tab="Support" view="PCMaint" title="PC Maintenance"> <description>Some Helpful tips for maintaining your PC</descriptio +n> <header>0</header> </link> ^; $xml .=qq^ <link tab="Support" view="Tempotutorials" title="Tempo Tutorials"> <description>Tempo Tutorials</description> <header>0</header> </link> <link tab="Support" view="TManuals" title="Tempo UserGuide"> <description>Tempo UserGuide</description> <header>0</header> </link>^ if ($Request->ServerVariables("SERVER_NAME")->item()=~/tempoh +elp/i); $xml .= qq^ <link tab="Support" view="Manuals" title="MLXchange Manuals"> <description>Documentation of MLXchange Software</description> <header>0</header> </link> <link tab="Support" view="FAQ" title="Frequently Asked Questions"> <description>Answers to the Most Common Questions</description> <header>0</header> </link> ^ if ( $Request->ServerVariables("SERVER_NAME")->item() =~ /mlxhelp/i +); $xml .= qq^ </leftIndex> ^; if (!$Request->item("View")->item() && $xml =~ /(.*?)<link tab +=\"Support\" view=\"Search\" .*?<\/link>(.*)/s) { $xml = $1.$2; } $xml =~ s/(<\/leftIndex>)/$logoutLink$1/s; $str .= $xml; last SWITCH; }

I get following error when I try to run this. Error Code: -1072896659 Reason: End tag 'leftIndex' does not match the start tag 'body'. Line: 2820

Replies are listed 'Best First'.
Re: outputting xml
by almut (Canon) on May 08, 2008 at 00:31 UTC
    End tag 'leftIndex' does not match the start tag 'body'

    Apparently, your generated XML is not well-formed...  My first guess would be that it's because you've commented out this <leftIndex> start tag:

    # Inserts leftIndex tag for document # #$xml = qq^<leftIndex>^;
Re: outputting xml
by GrandFather (Saint) on May 08, 2008 at 02:10 UTC

    Either you have a copy error or you need to use strictures (use strict; use warnings;). You use $xml in the block starting if (($Request->item ("Tab") ... $selected = "CTS")). It is not declared using my so is global and thus concatenates new material (each time the block is processed) onto whatever was left last time.

    The outer block labeled SWITCH seems redundant to me as there is no looping construct and the only explicit exit from the block is at the end in any case.


    Perl is environmentally friendly - it saves trees
Re: outputting xml
by pc88mxer (Vicar) on May 08, 2008 at 04:43 UTC
    First of all, the assignment to $selected in this if statement looks very suspect:
    if (($Request->item("Tab")->item() eq "Support") and ($selected + ="CTS"))
    Did you want $selected eq "CTS"?

    Secondly, I think you are choosing a difficult approach to generating XML. Even though XML is text, it really represents a data structure. A much easier way to create XML is to first create the data structure and then have a module like XML::Simple generate the XML for you. XML looks deceptively simple to produce and parse, and yet it has a lot of rules that need to be followed. Any simple-minded approach to generating XML is bound to have problems once you try to make it work for more general situations. It's much better to use a module which knows about all the formatting rules and will always produce correct XML.

Re: outputting xml
by CountZero (Bishop) on May 08, 2008 at 09:11 UTC
    Just as it is a bad idea to parse HTML with home-brewn regexes, for anything but the most trivial XML-files, don't even think of writing such files by simply print-ing them. It is far too easy to get it wrong in thousand subtle ways.

    Modules such as XML::Writer or XML::ValidWriter are a much better bet. It is not for nothing that XML-files have a DOCTYPE which can be used to check the validity of the file. XML::ValidWriter uses the DOCTYPE to make sure it writes a valid XML-file.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://685350]
Approved by pc88mxer
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-25 21:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found