#!/usr/bin/perl -w use lib '/usr/local/apache/cgi-bin/PXSQL'; use strict; use CGI qw(:standard); use PXSQL::XSQL; use PXSQL::XCGI; use XML::Sablotron qw( :all ); use DBI; use DBD::Pg; use PXSQL::EXSQL::ARTICLE; # Set the base directory for all XSQL and XSL templates. This path will # be prepend to aliases xml files my $XSQL_ROOT_DIR = '/usr/local/apache/cgi-bin/PXSQL/examples'; my $XSLT_ROOT_DIR = '/usr/local/apache/cgi-bin/PXSQL/examples'; # File containing the definition of the template files # and alias / CGI parameters association my $ALIAS_FILE = './template.lst'; my $ALIAS_CGI = './cgi_param.lst'; # Default template to use if not set my $DEFAULT_TEMPLATE = 'index'; # Do not bufferize $| = 1; my $cgi = new CGI; # Set the default template to process my $template = $cgi->param('template') || $DEFAULT_TEMPLATE; # Load all aliases between XSL templates files ans XML/SQL files # This return a hash of array as follow: # 'alias_name' => (xsl_file, xml_sql_file) my %aliases = &PXSQL::XSQL::load_templates($ALIAS_FILE); # Display an error message when a template doesn't exists if ( !exists $aliases{$template} ) { print $cgi->header(-type=>'text/html', -status => '404 Not Found'), $cgi->start_html(-title=>'404 Not Found'), qq{

Not Found

The requested URL $ENV{SCRIPT_NAME}?template=$template was not found on this server.


}; exit 1; } # Connect to the database my $dbh = DBI->connect( "dbi:Pg:dbname=xsql_test;host=localhost;port=5432", 'test', 'test', { RaiseError => 0 } ); # Create e new instance of the XSQL perl module my $xsql = new PXSQL::XSQL($dbh); # Set the directory where XSQL files can be found. This path will be # prepend to aliases xml/sql files $xsql->set_xml_sql_documents($XSQL_ROOT_DIR); # Create a new instance of the XCGI module allowing the save CGI # state into XML files. If you dont want to save all CGI parameters # create a file with all wanted CGI parameter for each XML file. my $xcgi = new PXSQL::XCGI($ALIAS_CGI); # Get XML output resulting from the SQL queries my $xmldata = $xsql->get_xml_data($cgi, $aliases{$template}[1] || ''); # Save CGI state to the XML file $xmldata .= $xcgi->getData($cgi, $aliases{$template}[1] || ''); print STDERR $xmldata, "\n"; # REDIRECT # You can redirect to other XSL template after XSQL execution. # This is usefull if you have insert/update statement and want # to switch to other template after that. You simply have to # add a cgi parameters called 'redirect'. if ( $cgi->param('redirect') ) { my $new = $cgi->param('redirect') || 'index'; print $cgi->redirect("http://localhost/cgi-bin/PXSQL/examples/perl_xsql.pl?template=$new"); exit 0; } print $cgi->header(); # Get the XSLT file content my $xsldata = ""; undef $/; open(GGG, "$XSLT_ROOT_DIR/$aliases{$template}[0]") or die "perl_xsql error: can't open $XSLT_ROOT_DIR/$aliases{$template}[0], $!\n"; $xsldata = ; close(GGG); $/ = "\n"; # Now use XML::Sablotron to process XSLT file and output HTML # You can use any other XSLT processor like gnome libxml my $result = ''; my $err = &XML::Sablotron::ProcessStrings($xsldata, $xmldata, $result); if ($err) { die "Perl_xsql error: $err\n"; } print $result; exit 0;