XML and modern CGI applications

Source: Internet
Author: User
Tags contains file system xslt

Brief introduction

The popularity of Perl has a direct relationship with the flourishing of the Internet. Perl's powerful features and easy to expand features make it the most natural choice for developing CGI applications, and thus quickly become the preferred language for CGI scripts. CGI itself is not perfect. But thanks to the favor of many developers, CGI is still widely used, and there is no indication that it will "retire" in the near future.

A typical cgi::xmlapplication script consists of three parts: a small executable script that provides access to the application, a logical module that implements various management methods, and possibly one or more XSLT stylesheets depending on the state of application. An XSLT stylesheet can translate the results returned by a module into a format that the browser can display to the user.

Here we will briefly introduce the application of cgi::xmlapplication by example.

Example 1:cgi XSLT Gateway

Cgi::xmlapplication assumes that the design and developers involved in a project use the XSLT stylesheet to separate the logic and representations of the application, so that the separation is straightforward and does not affect the project. Developers can simply make Setstylesheet return the location of the XSLT style sheet that matches the current application state. The conversion of the established DOM tree, the transfer of XSLT parameters to the transformation engine, and the transfer of content to the browser after conversion are transparent to the user.

To focus on this separation, our first example is not a traditional Web application, but rather a generic XSLT gateway that can be added to the server's Cgi-bin, transforming the entire XML content directory tree into a browser-compliant format, all of which for users, The style sheet and the author of the document are also transparent.

The first step is to establish a CGI script that connects the client's request and application. We want XML documents to be easy to navigate through URLs and to make hyperlinks between these documents very intuitive. Therefore, we will create a CGI script without an extension so that it can be used as a node in the URL path, and all content on the right side of the node will be interpreted in the context of the virtual document that contains the XML content. In this case, we call the CGI a stylesheet selector.

use strict;
use lib '/path/to/secure/webapp/libs';
use XSLGateway;
use CGI qw(:standard);my $q = CGI->new();
my %context = ();
my $gateway_name = 'stylechooser';

After loading the appropriate modules and setting some variables that are valid throughout the scope of the script, we begin to add some fields to the%context that is passed to the class that handles the application logic. In this application, we only transfer the requested URL (request entry) to the right of the script file path and the style key that contains the data stored in the query parameter style.

$context{REQUEST} = $q->url(-path => 1);
$context{REQUEST} =~ s/^$gateway_name/?//;
$context{REQUEST} ||= 'index.xml';
$context{STYLE} = $q->param ('style') if $q->param('style');

Finally, we create an instance of the Xslgateway logical class and use the% context as the only parameter by invoking its run method to process the request.

my $app = XSLGateway->new();
$app->run(%context);

The CGI script is done. Here we create the Xslgateway module that completes most of the work:

package XSLGateway;
use strict;
use vars qw(@ISA);
use CGI::XMLApplication;
use XML::LibXML;
@ISA = qw(CGI::XMLApplication);

As I mentioned in the introduction, Cgi::xmlapplication works through event invocations: The execution of a given method in an application class relies on the input of a specified field (usually the name of the button used to submit the form). ), you must perform two invocation methods: Selectstylesheet and Requestdom methods.

Selectstylesheet returns the full file system path for the XSLT style sheet. For simplicity's sake, we assume that the stylesheet will be saved in a single table of contents. We can increase the flexibility of the system by providing additional stylesheets through the $context-> {style} field.

sub selectStylesheet {
my $self = shift;
my $context = shift;
my $style = $context->{STYLE} || 'default';
my $style_path = '/opt/www/htdocs/stylesheets/';
return $style_path . $style . '.xsl';
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.