use strict;
use warnings;
use Test::More tests => 5; # last test to print
use XML::XPathScript;
use XML::XPathScript::Template;
use XML::XPathScript::Processor;
# rename
my $xml = 'ttt';
my $stylesheet = q#<% $t->set( foo => { rename => 'bar' } ) %><%~ / %>#;
my $xps = XML::XPathScript->new;
is $xps->transform( $xml => $stylesheet )
=> 'ttt', 'rename tag';
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
my $processor = $xps->processor;
my $template = XML::XPathScript::Template->new;
$processor->set_template( $template );
$xps->set_xml( <
END_XML
$template->set( foo => {
showtag => 1,
pre => '[pre {name()}]',
intro => '[intro {name()}]',
prechildren => '[prechildren {name()}]',
prechild => '[prechild {name()}]',
postchild => '[postchild {name()}]',
postchildren => '[postchildren {name()}]',
extro => '[extro {name()}]',
post => '[post {name()}]',
} );
# adding the "\n" is a kludge to use the <apply_templates()."\n" => <
[pre foo][intro foo][prechildren foo]
[prechild one][postchild one]
[prechild two][postchild two]
[prechild three][postchild three]
[postchildren foo][extro foo][post foo]
END_EXPECTED
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$template = XML::XPathScript::Template->new;
$processor->set_template( $template );
$template->set( foo => {
showtag => 1,
map { $_ => gen_sub( $_ ) }
qw/ pre intro prechildren prechild postchild postchildren extro post /
} );
sub gen_sub {
my $tag = shift;
return sub {
my ( $n, $t, $p ) = @_;
my $name = $n->findvalue( 'name()' );
return "#$tag $name $p->{p}#";
}
}
is $processor->apply_templates( { p => '!' } )."\n" => <
#pre foo !##intro foo !##prechildren foo !#
#prechild one !##postchild one !#
#prechild two !##postchild two !#
#prechild three !##postchild three !#
#postchildren foo !##extro foo !##post foo !#
END_EXPECTED
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$template = XML::XPathScript::Template->new;
$processor->set_template( $template );
$xps->set_xml( 'palyndrome' );
$template->set( 'text()' => {
pre => sub {
my ( $n, $t, $p ) = @_;
return reverse $n->findvalue( 'string()' );
}
} );
is $processor->apply_templates() => 'emordnylap',
'text() with function pre';
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$template = XML::XPathScript::Template->new;
$processor->set_template( $template );
$xps->set_xml( '' );
$template->set( 'comment()' => {
action => $DO_SELF_ONLY,
pre => sub {
my ( $n, $t, $p ) = @_;
return reverse $n->findvalue( 'string()' );
}
} );
is $processor->apply_templates() => ' emordnylap ',
'comment() with function pre';