File: 24namespaces.t

package info (click to toggle)
libxml-xpath-perl 1.44-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 588 kB
  • sloc: perl: 4,397; xml: 34; makefile: 2
file content (56 lines) | stat: -rw-r--r-- 1,252 bytes parent folder | download | duplicates (6)
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>