Return To Web Portfolio >

XML With Perl: Perl CGI Source Code

Example of Perl CGI code used to process the XML and XSL files.


#!/usr/bin/perl -w

use XML::XSLT;
my ($xmlflag, $xml, $xslfile, $xslstring, $parser, $action);

my $xmlfile     = "amazon.xml";
my $defaultxsl  = "amazon.xsl";
my $getxsl      = "get.xsl";
my $subjectxsl  = "bysubject.xsl";

&setup;
&parse;
&output;
exit(0);
...

sub parse() {
    unless ($xml) { open (XMLFILE,"<$xmlfile") || die $! . " " . $xmlfile; }
    if ($xslfile) { open (XSLFILE,"<$xslfile") || die $! . " " . $xslfile; }
    
    ########################################################################################################
    ## XSL PROCESSING
    
    if ($xslstring) { $parser = XML::XSLT->new ($xslstring, "STRING"); }
    elsif ($xslfile) { $parser = XML::XSLT->new (*XSLFILE, FILEHANDLE); }
    else { print "Content-type: text/html\n\n"; print "ERROR: No XSL passed to script.<BR><BR>"; exit(0); }
    ########################################################################################################
    
    if ($xml) { $parser->transform_document ($xml, STRING); }
    else      { $parser->transform_document (*XMLFILE, FILEHANDLE); }
    
    close XSLFILE;
    close XMLFILE;
} # end sub


Return To Web Portfolio >