File: 07blankdoc.t

package info (click to toggle)
libxml-libxslt-perl 1.66-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 468 kB
  • ctags: 733
  • sloc: perl: 798; ansic: 425; makefile: 51; xml: 21
file content (77 lines) | stat: -rw-r--r-- 1,888 bytes parent folder | download | duplicates (3)
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
use Test;
BEGIN { plan tests => 5 }
use XML::LibXSLT;

my $parser = XML::LibXML->new();
my $xslt = XML::LibXSLT->new();
ok($parser); ok($xslt);

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 $source = $parser->parse_string(<<'EOT','/foo');
<?xml version="1.0" encoding="ISO-8859-1"?>
<document></document>
EOT

my $foodoc = <<'EOT';
<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:data="data.uri" version="1.0">
<xsl:output encoding="ISO-8859-1" method="text"/>

<data:type>typed data in stylesheet</data:type>

<xsl:template match="/*">

Data: <xsl:value-of select="document('')/xsl:stylesheet/data:type"/><xsl:text>
</xsl:text>

</xsl:template>

</xsl:stylesheet>
EOT

my $style = $parser->parse_string($foodoc,'foo');

ok($style);
my $stylesheet = $xslt->parse_stylesheet($style);
# my $stylesheet = $xslt->parse_stylesheet_file("example/document.xsl");

my $results = $stylesheet->transform($source);
ok($results);

ok($results->toString, qr/typed data in stylesheet/);

###############################################################
# Callbacks - this is needed because with document('') now,
# libxslt expects to re-get the entire file and re-parse it,
# rather than its old behaviour, which was to use the internal
# DOM. So we have to use callbacks to be able to return the
# original file. We also need to make sure that the call
# to $parser->parse_string($foodoc, 'foo') gets a URI (second
# param), otherwise it doesn't know what to fetch.

sub match_cb {
    my $uri = shift;
    if ($uri eq 'foo') {
        return 1;
    }
    return 0;
}

sub open_cb {
    my $uri = shift;
    return \$foodoc;
}

sub close_cb {
}

sub read_cb {
    return substr(${$_[0]}, 0, $_[1], "");
}