Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

(jeffa) Re: XML for databases?!?! Is it just me or is the rest of the world nutz?

by jeffa (Bishop)
on May 24, 2002 at 06:42 UTC ( #168987=note: print w/replies, xml ) Need Help??

in reply to XML for databases?!?! Is it just me or is the rest of the world nutz?

I might not be on the same page here - XML for a database? Maybe XML instead of a database ...

Consider making a FAQ: there is a very generic layout for a FAQ. My idea was to use an HTML::Template template file to describe the layout, and use an XML file to contain the content. Sure i could have used a database, but then i would have to write some sort of a front end to enter in the data - why not use XML to wrap my data instead? It seemed easier to me.

Here is a stripped down version - it is not perfect and could probably use a revision, but it should be enough to demonstrate. As a matter of fact, i hear that you can even bypass having to use Perl to translate the XML directly to HTML - i'll hopefully learn more about that when my copy of Perl and XML arrives in the mail. (update - doh! i already knew about that - XSLT ;) - and so far that book is some very good reading)

There are a total of three files - save them with the suggested names and run the .pl file to generate the final HTML. You can redirect the output to an .html file, or even modify the script to be a CGI script instead.

<ol> <tmpl_loop name="category"> <li><a href="#<tmpl_var name=anchor>"><tmpl_var name="type"></a> <ol> <tmpl_loop name="qa_pair"> <li><tmpl_var name="question"> </tmpl_loop> </ol> </tmpl_loop> </ol> <p><hr/></p> <tmpl_loop name="category"> <a name="<tmpl_var name=anchor>"><h3><tmpl_var name="type"></h3></a> <tmpl_loop name="qa_pair"> <h4><tmpl_var name="question"></h4> <p><tmpl_var name="answer"></p> </tmpl_loop> <hr/> </tmpl_loop>
<xml> <category type="Generic Stuff"> <qa_pair> <question>How do I do stuff?</question> <answer> Just do it. Stuff is best done by doing it. By not doing it, stuff doesn't happen. </answer> </qa_pair> <qa_pair> <question>What do I do with this stuff?</question> <answer> Stuff. You do stuff with stuff. That way the stuff is used. </answer> </qa_pair> </category> <category type="Specific Stuff"> <qa_pair> <question>Do I really have to do stuff?</question> <answer> Of course not, you can just sit there without doing anything. Well you do have to breathe and eat and use Data::Dumper here and there. Stuff is good. Don't be afraid of stuff. </answer> </qa_pair> <qa_pair> <question>My girlfriend says stuff. Should we break up?</question> <answer> Of course not. It is common for a spouse to say stuff, you should encourage them and stuff. </answer> </qa_pair> </category> </xml>
use strict; use HTML::Template; use XML::Simple; my $xml = XMLin('./faq.xml') or die $!; my $template = HTML::Template->new( filename => 'faq.tmpl', die_on_bad_params => 0, ); # this could definitely use some refactoring - oh well ;) foreach my $cat (@{$xml->{'category'}}) { $cat->{'anchor'} = $cat->{'type'}; $cat->{'anchor'} =~ s/ /_/g; $cat->{'anchor'} =~ s/[^\w]//g; } $template->param( category => $xml->{'category'}, ); print $template->output();
You can see the real deal at

Hope this is on topic ...


(the triplet paradiddle with high-hat)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://168987]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2023-12-07 14:39 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (32 votes). Check out past polls.