File: xmlxpath_24namespaces.t

package info (click to toggle)
libxml-twig-perl 1%3A3.34-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,036 kB
  • ctags: 806
  • sloc: perl: 18,640; xml: 202; makefile: 18
file content (139 lines) | stat: -rw-r--r-- 3,557 bytes parent folder | download
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/perl -w

use strict;

use FindBin qw($Bin); BEGIN { unshift @INC, $Bin; } use xmlxpath_tools;

use Test;

if( defined $XML::XPathEngine::VERSION && $XML::XPathEngine::VERSION < 0.09)
  { print "1..1\nok 1\n"; 
    warn "cannot use set_namespace, needs XML::XPathEngine 0.09+ (installed version is $XML::XPathEngine::VERSION)\n"; 
    exit;
  }

plan( tests => 14);


my $t= XML::Twig::XPath->new->parse( *DATA);

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

my @nodes;

# Do not set namespace prefixes - uses element context namespaces

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

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

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

$node= $t->findvalue( '//*[@attr:findme]');
ok( $node, 'attr content');

# Set namespace mappings.


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

@nodes = $t->findnodes('//foo:foo', $t); # should find flubber.com foos
ok( @nodes, 2);

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

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

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

## added to test set_namespace
if( ! defined $XML::XPathEngine::VERSION )
  { my_skip( 5, "can only test set_strict_namespaces with XML::XPathEngine 0.09+ installed"); }
else
  { my $xml= '<root xmlns="http://example.com/">
                <node>
                   <foo>Node 1</foo>
                   <bar xmlns:ex="http://example.com/">
                      <ex:foo>Node 2</ex:foo>
                   </bar>
                </node>
             </root>
            ';
    {
      my $twig = XML::Twig::XPath->new();
      $twig->parse( $xml); 
      $twig->set_namespace('example','http://example.com/');
      $twig->set_strict_namespaces(1);
      my $v = $twig->findvalue('//foo');
      ok( $v, '', '//foo (strict_namespaces)');
      $twig->set_strict_namespaces(0);
      my $v1 = $twig->findvalue('//foo');
      ok( $v1, 'Node 1', '//foo (default behaviour)');
    }
    
    
    {
      my $twig = XML::Twig::XPath->new();
      $twig->set_namespace('example','http://example.com/');
      $twig->parse( $xml); 
    
      my $v = $twig->findvalue('//example:foo');
      ok( $v, 'Node 1Node 2', '//example:foo');
    }
    
    {
      my $twig = XML::Twig::XPath->new();
      $twig->parse( $xml); 
      my $v = $twig->findvalue('//foo');
      ok( $v, 'Node 1', '//foo');
    }
    
    {
      my $twig = XML::Twig::XPath->new();
      $twig->parse( $xml); 
      $twig->set_namespace('example','http://example.com/');
      my $v = $twig->findvalue('//foo');
      ok( $v, 'Node 1', '//foo (default behaviour)');
    }
  }
 
{       
my %seen_message;
  sub my_skip
    { my( $nb_skip, $message)= @_;
      $message ||='';
      unless( $seen_message{$message})
        { warn "\n$message: skipping $nb_skip tests\n";
          $seen_message{$message}++;
        }
      for (1..$nb_skip) { ok( 1); }
    }
}

        
    
exit 0;

__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">attr content</attr:node >
</xml>