[XML4Lib] server-side XML and XSLT
Eric Lease Morgan
emorgan at nd.edu
Wed Aug 16 11:15:07 EDT 2006
On Aug 16, 2006, at 10:21 AM, Pierre Nault wrote:
> I'm sorry for this naive question but, what do I need to do that
> sort of work on the server-side.
I have wrestled with this problem too. I want to deliver XML, but I
can not trust the client to transform the XML correctly.
Consequently, I have, on a number of occasions, transformed the XML
on the server and returned HTML. I have used two solutions, both
using Perl:
1. AxKit - This is/was a cool mod_perl module. It worked, and worked
well, but I haven't seen nor heard of about it for a while. I used it
to do my water collection:
http://infomotions.com/water/
The only things driving this implementation are a single XML file (of
my own design), a single XSLT file for transformation, and AxKit.
2. XML::LibXML & XML::LibXSLT - This combination has proven to be
more practical, especially in regards to our implementations of SRU.
Searches applied against SRU servers return streams of XML. I can not
expect browsers to support XML transformation. Consequently, our SRU
clients transform the response into HTML before sending it back. The
heart of this process is the creation of an XSLT file munged with the
SRU result. The code looks like the following but the technique
should applicable to just about programming language for server-side
processing:
# require modules
use CGI;
use LWP::UserAgent;
use XML::LibXML;
use XML::LibXSLT;
# define constants
use constant QUERY => 'origami or "paper folding"';
use constant SRU2HTML => 'etc/sru2html.xsl';
use constant SRUROOT => 'http://example.com/.sru.cgi?
operation=searchRetrieve&version=1.1&query=';
# initialize
my $cgi = CGI->new;
my $html = '';
# create a user agent, create a request, send it, and get a response
my $ua = LWP::UserAgent->new(agent => 'SRU-Client/0.1 ');
my $request = HTTP::Request->new(GET => SRUROOT . QUERY);
my $response = $ua->request($request);
# check response
if ($response->is_success) {
# create parser, validate response as well as stylesheet
my $parser = XML::LibXML->new;
my $xslt = XML::LibXSLT->new;
my $source = $parser->parse_string($response->content) or
croak $!;
my $style = $parser->parse_file(SRU2HTML) or
croak $!;
# transform; the reason de existance
my $stylesheet = $xslt->parse_stylesheet($style) or
croak $!;
my $results = $stylesheet->transform($source) or
croak $!;
# update the output
$html = $stylesheet->output_string($results);
}
# error; bummer
else { $html = $response->status_line, "\n" }
# return the results to the client; done
print $cgi->header(-type => 'text/html', -charset => 'utf-8');
print $html;
exit;
Fun!
--
Eric Morgan
University Libraries of Notre Dame
More information about the XML4Lib
mailing list