| 1 | package XML::Schematron::XSLTProcessor; | ||
| 2 | use Moose::Role; | ||
| 3 | use namespace::autoclean; | ||
| 4 | with 'XML::Schematron::Schema'; | ||
| 5 | |||
| 6 | has template_buffer => ( | ||
| 7 | traits => ['String'], | ||
| 8 | is => 'rw', | ||
| 9 | isa => 'Str', | ||
| 10 | default => sub { return '' }, | ||
| 11 | handles => { | ||
| 12 | append_template => 'append', | ||
| 13 | reset_template => 'clear', | ||
| 14 | }, | ||
| 15 | |||
| 16 | ); | ||
| 17 | |||
| 18 | sub tests_to_xsl { | ||
| 19 | my $self = shift; | ||
| 20 | my $mode = 'M0'; | ||
| 21 | my $ns = qq|xmlns:xsl="http://www.w3.org/1999/XSL/Transform"|; | ||
| 22 | |||
| 23 | $self->append_template( | ||
| 24 | qq|<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
| 25 | <xsl:stylesheet $ns version="1.0"> | ||
| 26 | <xsl:output method="text"/> | ||
| 27 | <xsl:template match="/"> | ||
| 28 | <xsl:apply-templates select="/" mode="$mode"/>| | ||
| 29 | ); | ||
| 30 | |||
| 31 | |||
| 32 | my $last_context_path = ''; | ||
| 33 | my $priority = 4000; | ||
| 34 | foreach my $test ( $self->all_tests) { | ||
| 35 | |||
| 36 | if ($test->context ne $last_context_path) { | ||
| 37 | $self->append_template( | ||
| 38 | qq|\n<xsl:apply-templates mode="$mode"/>\n| | ||
| 39 | ) unless $priority == 4000; | ||
| 40 | |||
| 41 | $self->append_template( | ||
| 42 | sprintf(qq|</xsl:template>\n<xsl:template match="%s" priority="$priority" mode="$mode">|, $test->context) | ||
| 43 | ); | ||
| 44 | $priority--; | ||
| 45 | } | ||
| 46 | |||
| 47 | $self->append_template( $test->as_xsl ); | ||
| 48 | |||
| 49 | $last_context_path = $test->context; | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | $self->append_template( | ||
| 54 | qq|<xsl:apply-templates mode="$mode"/>\n</xsl:template>\n | ||
| 55 | <xsl:template match="text()" priority="-1" mode="M0"/> | ||
| 56 | </xsl:stylesheet>| | ||
| 57 | ); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub dump_xsl { | ||
| 61 | my $self = shift; | ||
| 62 | $self->tests_to_xsl; | ||
| 63 | return $self->template_buffer; | ||
| 64 | } | ||
| 65 | |||
| 66 | 1; |