note
jeffa
I might not be on the same page here - XML for a database?
Maybe XML instead of a database ...
<p>
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.
<p>
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
[isbn://059600205X|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)
<p>
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.
<p><readmore></p>
<b>faq.tmpl</b>
<code>
<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>
</code>
<b>faq.xml</b>
<code>
<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>
</code>
<b>faq.pl</b>
<code>
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();
</code>
You can see the real deal at [http://unlocalhost.com/XHTML_Table/FAQ.html].
<p>
Hope this is on topic ...
<p>jeffa</p>
<font size=1>
<pre>
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---
(<a href="http://jeffa.perlmonk.org/tripdid.mp3">the triplet paradiddle with high-hat</a>)
</pre></font>
168977
168977