B
Bart Van der Donck
Jomo said:How do I generate a request from an HTML search form
(or button/hyperlink) to search/query an XML data file,
using CGI scripts, and have the results displayed within
an HTML page?
Here is a (unix) starting point to play around with. It uses Perl's
XML::Simple (http://search.cpan.org/dist/XML-Simple/).
Say 3 files in your same directory:
(1) start.html
(2) data.xml
(3) script.pl
-------------------------------------------
(1) start.html
-------------------------------------------
<html>
<body>
<form method=get action=script.pl>
What is the website of
<input type=text name=artist>
<input type=submit value=Search>
</form>
</body>
</html>
-------------------------------------------
(2) data.xml
-------------------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<ARTISTWEBSITES>
<RECORD>
<ARTIST>Bob Dylan</ARTIST>
<URL>http://www.bob-dylan.com</URL>
</RECORD>
<RECORD>
<ARTIST>Bonnie Tyler</ARTIST>
<URL>http://www.bonnie-tyler.com</URL>
</RECORD>
<RECORD>
<ARTIST>Dolly Parton</ARTIST>
<URL>http://www.dolly-parton.com</URL>
</RECORD>
</ARTISTWEBSITES>
-------------------------------------------
(3) script.pl
-------------------------------------------
#!/usr/bin/perl
print "Content-Type: text/html\n\n<html>\n<body>\n";
use CGI::Carp qw(fatalsToBrowser);
use XML::Simple;
my $found=0;
my $q=$ENV{"QUERY_STRING"};
$q=~s/^artist=//;
$q=~tr/+/ /;
$q=~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
my $xmlfile = XMLin("data.xml", forcearray=>1);
for my $record (@{$xmlfile->{RECORD}})
{
if (uc $record->{"ARTIST"}->[0] eq uc $q)
{
print $record->{"URL"}->[0]; $found=1;
}
}
print "No website found" if ($found!=1);
print "\n</body>\n</html>";
XML::Simple is easy and of good quality, but it has a limitation
though for your xml data regarding so-called 'mixed content'
(<para>This is <em>mixed</em> content.</para>).
You find more info at see http://search.cpan.org/dist/XML-Simple/. It
also says that very large XML files should not be approached by
XML::Simple due to memory issues.
HTH