| 1 | #!/usr/local/bin/perl | ||
| 2 | use Moose; | ||
| 3 | use Try::Tiny; | ||
| 4 | use Class::Load; | ||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | xxslt.pl - load Perl extension classes while performing XSLT transformations. | ||
| 8 | |||
| 9 | =head1 VERSION | ||
| 10 | |||
| 11 | version 1.140260 | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | xxslt.pl My::Extenstion::Class stylesheet xml | ||
| 16 | |||
| 17 | Example: | ||
| 18 | |||
| 19 | xxslt.pl My::Extension::Class mystyle.xsl source.xml | ||
| 20 | |||
| 21 | Or, you can read the XML to be transformed from STDIN by substituting a dash '-' for the file path: | ||
| 22 | |||
| 23 | generate_xml.sh | xxslt.pl My::Extension::Class mystyle.xsl - | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | C<xxlt.pl> is a command-line interface to L<Role::LibXSLT::Extender>, which allows you to easily register classes of Perl extension functions for use from within your XSLT stylesheets.. | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | |||
| 32 | my $extension_class = shift; | ||
| 33 | my $stylesheet_file = shift; | ||
| 34 | my $xml_file = shift; | ||
| 35 | my %xml_args = (); | ||
| 36 | |||
| 37 | # sanity checking | ||
| 38 | die usage() unless $extension_class && $stylesheet_file && $xml_file; | ||
| 39 | |||
| 40 | # use the typical convention for reading from STDIN | ||
| 41 | if ( $xml_file eq '-' ) { | ||
| 42 | while( <STDIN> ) { | ||
| 43 | $xml_args{string} .= $_; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | else { | ||
| 47 | die "XML document '$xml_file' does not exist.\n" unless -f $xml_file; | ||
| 48 | $xml_args{location} = $xml_file; | ||
| 49 | } | ||
| 50 | |||
| 51 | # load the extension class | ||
| 52 | try { | ||
| 53 | Class::Load::load_class( $extension_class ); | ||
| 54 | } | ||
| 55 | catch { | ||
| 56 | die "Couldn't load extension class '$extension_class': $_ \n"; | ||
| 57 | }; | ||
| 58 | |||
| 59 | die "Styleheet file '$stylesheet_file' does not exist.\n" unless -f $stylesheet_file; | ||
| 60 | |||
| 61 | my $xslt = $extension_class->new(); | ||
| 62 | |||
| 63 | my $style_dom = XML::LibXML->load_xml( location => $stylesheet_file ); | ||
| 64 | my $input_dom = XML::LibXML->load_xml( %xml_args ); | ||
| 65 | my $stylesheet = $xslt->parse_stylesheet($style_dom); | ||
| 66 | |||
| 67 | |||
| 68 | my $transd_input = $stylesheet->transform( $input_dom ); | ||
| 69 | print $stylesheet->output_as_bytes($transd_input); | ||
| 70 | |||
| 71 | exit(0); | ||
| 72 | |||
| 73 | sub usage { | ||
| 74 | die "$0 extension_class stylesheet_file xml_file\n"; | ||
| 75 | } |