Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

(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.

faq.tmpl
<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>
faq.xml
<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>
faq.pl
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 http://unlocalhost.com/XHTML_Table/FAQ.html.

Hope this is on topic ...

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)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://168987]
help
Chatterbox?
[choroba]: LanX I miss working in a bank sometimes...
[Corion]: Discipulus: Ooof. Especially yearly things are things I like to automate instead of trying to remember how I did things last year...
[Corion]: And the second rule that I've learned is, that there is no one-off job, so writing a program for it pays off almost immediately. The third rule is to give all my programs numbers and have them reproduce that number in the name of their output files. :)
[Discipulus]: the true part is that also specification change between years.. but well our job is cheap but dont abuse of us.. ;=)
[LanX]: Choroba: do you miss chaos with ties? apply at the US government.. ;)
[ambrus]: Corion: those are good rules.
[ambrus]: Discipulus: oh sure. the input data has different filenames every time I get them.
[ambrus]: the directory structure may be 1, 2, or 3 deep, it may have spaces in the filename or not, it has dates in various format, different keywords for the same meanings, and the dates and other keywords are assembled in various ways.

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (14)
As of 2017-03-29 12:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (350 votes). Check out past polls.