1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
use Test;
BEGIN { plan tests => 12 }
use XML::LibXSLT;
use XML::LibXML;
my $parser = XML::LibXML->new();
ok($parser);
my $doc = $parser->parse_string(<<'EOT');
<xml>random contents</xml>
EOT
ok($doc);
my $xslt = XML::LibXSLT->new();
ok($xslt);
# warn("setting callbacks\n");
local $XML::LibXML::match_cb = \&match_cb;
local $XML::LibXML::open_cb = \&open_cb;
local $XML::LibXML::close_cb = \&close_cb;
local $XML::LibXML::read_cb = \&read_cb;
my $stylesheet = $xslt->parse_stylesheet($parser->parse_string(<<'EOT'));
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
<html>
<head><title>Know Your Dromedaries</title></head>
<body>
<h1><xsl:apply-templates/></h1>
<p>foo: <xsl:apply-templates select="document('foo.xml')/*" /></p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
EOT
ok($stylesheet);
# warn "transform!\n";
my $results = $stylesheet->transform($doc);
ok($results);
my $output = $stylesheet->output_string($results);
# warn "output: $output\n";
ok($output);
$XML::LibXML::open_cb = \&dying_open_cb;
# check transform throws exception
eval {
$stylesheet->transform($doc);
};
if ($@) {
ok(1, 1, "Threw: $@");
}
else {
ok(0, 1, "No error");
}
sub match_cb {
my $uri = shift;
# warn("match: $uri\n");
if ($uri eq "foo.xml") {
ok(1);
return 1;
}
return 0;
}
sub open_cb {
my $uri = shift;
# warn("open $uri\n");
ok($uri, "foo.xml");
return "<foo>Text here</foo>";
}
sub dying_open_cb {
my $uri = shift;
ok($uri, "foo.xml");
die "Test a die from open_cb";
}
sub close_cb {
# warn("close\n");
ok(1);
}
sub read_cb {
# warn("read\n");
return substr($_[0], 0, $_[1], "");
}
|