| 1 | package XML::Schematron::XPath; | ||
| 2 | use Moose::Role; | ||
| 3 | use namespace::autoclean; | ||
| 4 | with 'XML::Schematron::Schema'; | ||
| 5 | |||
| 6 | use XML::XPath; | ||
| 7 | |||
| 8 | sub verify { | ||
| 9 | my $self = shift; | ||
| 10 | my $xml = shift; | ||
| 11 | |||
| 12 | my @messages = (); | ||
| 13 | |||
| 14 | $self->parse_schema if $self->has_schema; | ||
| 15 | |||
| 16 | #TODO: let 'em invisibly pass filehandles, too. | ||
| 17 | |||
| 18 | my $xp; | ||
| 19 | |||
| 20 | if ( $xml =~ /^\s*<\?\s*(xml|XML)\b/ ) { | ||
| 21 | $xp = XML::XPath->new(xml => $xml); | ||
| 22 | } | ||
| 23 | else { | ||
| 24 | $xp = XML::XPath->new(filename => $xml); | ||
| 25 | } | ||
| 26 | |||
| 27 | foreach my $test ( $self->all_tests) { | ||
| 28 | |||
| 29 | my $step_count = 0; | ||
| 30 | my $node_count = 0; | ||
| 31 | my $context = 0; | ||
| 32 | |||
| 33 | # make sure our search is global unless they specified a path | ||
| 34 | unless ( $test->context =~ m|^/| ) { | ||
| 35 | my $temp = $test->context; | ||
| 36 | $temp = qq|//$temp|; | ||
| 37 | $test->context($temp); | ||
| 38 | } | ||
| 39 | |||
| 40 | # here's the beef... | ||
| 41 | # it seems too simple (and probably is) :> | ||
| 42 | |||
| 43 | foreach my $node ($xp->findnodes($test->context)->get_nodelist) { | ||
| 44 | $step_count++ if $node->find($test->expression); | ||
| 45 | } | ||
| 46 | |||
| 47 | if ($test->test_type eq 'assert') { | ||
| 48 | my $dupe_node = $xp->findnodes($test->context); | ||
| 49 | $node_count = $dupe_node->size; | ||
| 50 | } | ||
| 51 | else { | ||
| 52 | $node_count = 0; | ||
| 53 | } | ||
| 54 | |||
| 55 | #print "node_count for $test is $node_count step_count is $step_count\n"; | ||
| 56 | |||
| 57 | if ($node_count != $step_count) { | ||
| 58 | push (@messages, sprintf("In pattern %s: %s", $test->pattern, $test->message) ); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | return wantarray ? @messages : join "\n", @messages; | ||
| 63 | } | ||
| 64 | |||
| 65 | 1; | ||
| 66 | __END__ |