| 1 | package XML::Schematron::LibXSLT; | ||
| 2 | |||
| 3 | use Moose::Role; | ||
| 4 | use namespace::autoclean; | ||
| 5 | |||
| 6 | use XML::LibXSLT; | ||
| 7 | use XML::LibXML; | ||
| 8 | |||
| 9 | |||
| 10 | has xml_parser => ( | ||
| 11 | is => 'ro', | ||
| 12 | isa => 'XML::LibXML', | ||
| 13 | required => 1, | ||
| 14 | default => sub { XML::LibXML->new(); }, | ||
| 15 | ); | ||
| 16 | |||
| 17 | has xslt_processor => ( | ||
| 18 | is => 'ro', | ||
| 19 | isa => 'XML::LibXSLT', | ||
| 20 | required => 1, | ||
| 21 | default => sub { XML::LibXSLT->new(); }, | ||
| 22 | ); | ||
| 23 | |||
| 24 | sub verify { | ||
| 25 | my $self = shift; | ||
| 26 | my $xml = shift; | ||
| 27 | |||
| 28 | $self->parse_schema if $self->has_schema; | ||
| 29 | |||
| 30 | my $template = $self->dump_xsl; | ||
| 31 | my $xml_doc; | ||
| 32 | |||
| 33 | if ( $xml =~ /^\s*<\?\s*(xml|XML)\b/ ) { | ||
| 34 | $xml_doc = $self->xml_parser->parse_string($xml); | ||
| 35 | } | ||
| 36 | else { | ||
| 37 | $xml_doc = $self->xml_parser->parse_file($xml); | ||
| 38 | } | ||
| 39 | |||
| 40 | my $style_doc = $self->xml_parser->parse_string($template); | ||
| 41 | |||
| 42 | my $stylesheet = $self->xslt_processor->parse_stylesheet($style_doc); | ||
| 43 | my $result = $stylesheet->transform($xml_doc); | ||
| 44 | my $ret_string = $stylesheet->output_as_bytes($result); | ||
| 45 | |||
| 46 | if (wantarray) { | ||
| 47 | my @ret_array = split "\n", $ret_string; | ||
| 48 | return @ret_array; | ||
| 49 | } | ||
| 50 | |||
| 51 | return $ret_string; | ||
| 52 | } | ||
| 53 | |||
| 54 | with 'XML::Schematron::XSLTProcessor'; | ||
| 55 | |||
| 56 | 1; | ||
| 57 | |||
| 58 | __END__ |