#!/usr/bin/perl -w # # $Id: xslt-simple.pl,v 1.3 2003/01/13 05:28:43 jmates Exp $ # # Copyright (c) 2001, Jeremy Mates. This script is free software; # you can redistribute it and/or modify it under the same terms as # Perl itself. # # Run perldoc(1) on this file for additional documentation. # ###################################################################### # # REQUIREMENTS require 5; use strict; ###################################################################### # # MODULES use Carp; # better error reporting use Getopt::Std; # command line option processing use XML::LibXSLT; use XML::LibXML; ###################################################################### # # VARIABLES my $VERSION; ($VERSION = '$Revision: 1.3 $ ') =~ s/[^0-9.]//g; my (%opts); ###################################################################### # # MAIN # parse command-line options getopts('h?', \%opts); help() if exists $opts{'h'} or exists $opts{'?'}; # read from STDIN if no args left chomp(@ARGV = ) unless @ARGV; # and flag the help text if nothing from STDIN help() unless @ARGV; # accept XML file, XSL file as arguments (or subsequent STDIN lines) my $xml_file = shift or help(); my $xsl_file = shift or help(); # parse and print! # might want to add exception handling of some sort, but hey! my $parser = XML::LibXML->new(); my $xslt = XML::LibXSLT->new(); my $source = $parser->parse_file($xml_file); my $style_doc = $parser->parse_file($xsl_file); my $stylesheet = $xslt->parse_stylesheet($style_doc); my $results = $stylesheet->transform($source); print $stylesheet->output_string($results); exit; ###################################################################### # # SUBROUTINES # a generic help blarb sub help { print <<"HELP"; Usage: $0 [opts] xml-file xslt-file Simple script to apply XSLT to xml-file, results to STDOUT. Options for version $VERSION: -h/-? Display this message Run perldoc(1) on this script for additional documentation. HELP exit; } ###################################################################### # # DOCUMENTATION =head1 NAME xslt-simple.pl - applies XSLT to XML files =head1 SYNOPSIS $ xslt-simple.pl foo.xml foo-munger.xsl =head1 DESCRIPTION =head2 Overview Simple script primarly ripped from XML::LibXSLT SYNOPSIS documentation to allow me to easily transform documents in command line environment. =head2 Normal Usage $ xslt-simple.pl [options] xml-file xslt-file If required command line arguments are omitted, the script will attempt to read the xml and xslt filenames from STDIN. Output is to STDOUT. The script will die should errors be encountered during processing. See L<"OPTIONS"> for details on the command line switches supported. =head1 OPTIONS This script currently supports the following command line switches: =over 4 =item B<-h>, B<-?> Prints a brief usage note about the script. =back =head1 ENVIRONMENT Requires XML::LibXSLT, plus supporting related perl modules and Gnome libraires to be installed first. =head1 BUGS =head2 Reporting Bugs Newer versions of this script may be available from: http://sial.org/code/perl/ If the bug is in the latest version, send a report to the author. Patches that fix problems or add new features are welcome. =head2 Known Issues Unexplored are the various failure modes, e.g. how XML::LibXSLT dies, what one can except, and whether eval statments to handle such stuff gracefully are required or not. =head1 SEE ALSO perl(1) =head1 AUTHOR Jeremy Mates, http://sial.org/contact/ =head1 COPYRIGHT Copyright (c) 2001, Jeremy Mates. This script is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 VERSION $Id: xslt-simple.pl,v 1.3 2003/01/13 05:28:43 jmates Exp $ =cut