summaryrefslogtreecommitdiff
path: root/t/24namespaces.t
blob: b77f9dc4201ef6e7c9cd7aa71d9a8d8859389cb9 (plain)
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
use Test;
BEGIN { plan tests => 9 }

use XML::XPath;
ok(1);

my $xp = XML::XPath->new(ioref => *DATA);
ok($xp);

my @nodes;

# Don't set namespace prefixes - uses element context namespaces

@nodes = $xp->findnodes('//foo:foo'); # should find foobar.com foos
ok(@nodes, 3);

@nodes = $xp->findnodes('//goo:foo'); # should find no foos
ok(@nodes, 0);

@nodes = $xp->findnodes('//foo'); # should find default NS foos
ok(@nodes, 2);

# Set namespace mappings.

$xp->set_namespace("foo" => "flubber.example.com");
$xp->set_namespace("goo" => "foobar.example.com");

# warn "TEST 6\n";
@nodes = $xp->findnodes('//foo:foo'); # should find flubber.com foos
# warn "found: ", scalar @nodes, "\n";
ok(@nodes, 2);

@nodes = $xp->findnodes('//goo:foo'); # should find foobar.com foos
ok(@nodes, 3);

@nodes = $xp->findnodes('//foo'); # should find default NS foos
ok(@nodes, 2);

ok($xp->findvalue('//attr:node/@attr:findme'), 'someval');

__DATA__
<xml xmlns:foo="foobar.example.com"
    xmlns="flubber.example.com">
    <foo>
        <bar/>
        <foo/>
    </foo>
    <foo:foo>
        <foo:foo/>
        <foo:bar/>
        <foo:bar/>
        <foo:foo/>
    </foo:foo>
    <attr:node xmlns:attr="attribute.example.com"
        attr:findme="someval"/>
</xml>