Source

perl-XML-LibXML / t / 10ns.t

  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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# -*- cperl -*-
use Test;
BEGIN { plan tests=>129; }
use XML::LibXML;
use XML::LibXML::Common qw(:libxml);

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

my $xml1 = <<EOX;
<a xmlns:b="http://whatever"
><x b:href="out.xml"
/><b:c/></a>
EOX

my $xml2 = <<EOX;
<a xmlns:b="http://whatever" xmlns:c="http://kungfoo"
><x b:href="out.xml"
/><b:c/><c:b/></a>
EOX

my $xml3 = <<EOX;
<a xmlns:b="http://whatever">
    <x b:href="out.xml"/>
    <x>
    <c:b xmlns:c="http://kungfoo">
        <c:d/>
    </c:b>
    </x>
    <x>
    <c:b xmlns:c="http://foobar">
        <c:d/>
    </c:b>
    </x>
</a>
EOX

print "# 1.   single namespace \n";

{
    my $doc1 = $parser->parse_string( $xml1 );
    my $elem = $doc1->documentElement;
    ok($elem->lookupNamespaceURI( "b" ), "http://whatever" );
    my @cn = $elem->childNodes;
    ok($cn[0]->lookupNamespaceURI( "b" ), "http://whatever" );
    ok($cn[1]->namespaceURI, "http://whatever" );
}

print "# 2.    multiple namespaces \n";

{
    my $doc2 = $parser->parse_string( $xml2 );

    my $elem = $doc2->documentElement;
    ok($elem->lookupNamespaceURI( "b" ), "http://whatever");
    ok($elem->lookupNamespaceURI( "c" ), "http://kungfoo");
    my @cn = $elem->childNodes;

    ok($cn[0]->lookupNamespaceURI( "b" ), "http://whatever" );
    ok($cn[0]->lookupNamespaceURI( "c" ), "http://kungfoo");

    ok($cn[1]->namespaceURI, "http://whatever" );
    ok($cn[2]->namespaceURI, "http://kungfoo" );
}

print "# 3.   nested names \n";

{
    my $doc3 = $parser->parse_string( $xml3 );    
    my $elem = $doc3->documentElement;
    my @cn = $elem->childNodes;
    my @xs = grep { $_->nodeType == XML_ELEMENT_NODE } @cn;

    my @x1 = $xs[1]->childNodes; my @x2 = $xs[2]->childNodes;

    ok( $x1[1]->namespaceURI , "http://kungfoo" );    
    ok( $x2[1]->namespaceURI , "http://foobar" );    

    # namespace scopeing
    ok( not defined $elem->lookupNamespacePrefix( "http://kungfoo" ) );
    ok( not defined $elem->lookupNamespacePrefix( "http://foobar" ) );
}

print "# 4. post creation namespace setting\n";
{
    my $e1 = XML::LibXML::Element->new("foo");
    my $e2 = XML::LibXML::Element->new("bar:foo");
    my $e3 = XML::LibXML::Element->new("foo");
    $e3->setAttribute( "kung", "foo" );
    my $a = $e3->getAttributeNode("kung");

    $e1->appendChild($e2);
    $e2->appendChild($e3);
    ok( $e2->setNamespace("http://kungfoo", "bar") );
    ok( $a->setNamespace("http://kungfoo", "bar") );
    ok( $a->nodeName, "bar:kung" );
}

print "# 5. importing namespaces\n";

{

    my $doca = XML::LibXML->createDocument;
    my $docb = XML::LibXML->new()->parse_string( <<EOX );
<x:a xmlns:x="http://foo.bar"><x:b/></x:a>
EOX

    my $b = $docb->documentElement->firstChild;

    my $c = $doca->importNode( $b );

    my @attra = $c->attributes;
    ok( scalar(@attra), 1 );
    ok( $attra[0]->nodeType, 18 );
    my $d = $doca->adoptNode($b);

    ok( $d->isSameNode( $b ) );
    my @attrb = $d->attributes;
    ok( scalar(@attrb), 1 );
    ok( $attrb[0]->nodeType, 18 );
}

print "# 6. lossless setting of namespaces with setAttribute\n";
# reported by Kurt George Gjerde
{
    my $doc = XML::LibXML->createDocument; 
    my $root = $doc->createElementNS('http://example.com', 'document');
    $root->setAttribute('xmlns:xxx', 'http://example.com');
    $root->setAttribute('xmlns:yyy', 'http://yonder.com');
    $doc->setDocumentElement( $root );

    my $strnode = $root->toString();
    ok ( $strnode =~ /xmlns:xxx/ and $strnode =~ /xmlns=/ );
}

print "# 7. namespaced attributes\n";
{
  my $doc = XML::LibXML->new->parse_string(<<'EOF');
<test xmlns:xxx="http://example.com"/>
EOF
  my $root = $doc->getDocumentElement();
  # namespaced attributes
  $root->setAttribute('xxx:attr', 'value');
  ok ( $root->getAttributeNode('xxx:attr') );
  ok ( $root->getAttribute('xxx:attr'), 'value' );
  print $root->toString(1),"\n";
  ok ( $root->getAttributeNodeNS('http://example.com','attr') );
  ok ( $root->getAttributeNS('http://example.com','attr'), 'value' );
  ok ( $root->getAttributeNode('xxx:attr')->getNamespaceURI(), 'http://example.com');

  #change encoding to UTF-8 and retest
  $doc->setEncoding('UTF-8');
  # namespaced attributes
  $root->setAttribute('xxx:attr', 'value');
  ok ( $root->getAttributeNode('xxx:attr') );
  ok ( $root->getAttribute('xxx:attr'), 'value' );
  print $root->toString(1),"\n";
  ok ( $root->getAttributeNodeNS('http://example.com','attr') );
  ok ( $root->getAttributeNS('http://example.com','attr'), 'value' );
  ok ( $root->getAttributeNode('xxx:attr')->getNamespaceURI(), 'http://example.com');
}

print "# 8. changing namespace declarations\n";
{
  my $xmlns = 'http://www.w3.org/2000/xmlns/';

    my $doc = XML::LibXML->createDocument; 
    my $root = $doc->createElementNS('http://example.com', 'document');
    $root->setAttributeNS($xmlns, 'xmlns:xxx', 'http://example.com');
    $root->setAttribute('xmlns:yyy', 'http://yonder.com');
    $doc->setDocumentElement( $root );

    # can we get the namespaces ?
    ok ( $root->getAttribute('xmlns:xxx'), 'http://example.com');
    ok ( $root->getAttributeNS($xmlns,'xmlns'), 'http://example.com' );
    ok ( $root->getAttribute('xmlns:yyy'), 'http://yonder.com');
    ok ( $root->lookupNamespacePrefix('http://yonder.com'), 'yyy');
    ok ( $root->lookupNamespaceURI('yyy'), 'http://yonder.com');

    # can we change the namespaces ?
    ok ( $root->setAttribute('xmlns:yyy', 'http://newyonder.com') );
    ok ( $root->getAttribute('xmlns:yyy'), 'http://newyonder.com');
    ok ( $root->lookupNamespacePrefix('http://newyonder.com'), 'yyy');
    ok ( $root->lookupNamespaceURI('yyy'), 'http://newyonder.com');

    # can we change the default namespace ?
    $root->setAttribute('xmlns', 'http://other.com' );
    ok ( $root->getAttribute('xmlns'), 'http://other.com' );
    ok ( $root->lookupNamespacePrefix('http://other.com'), "" );
    ok ( $root->lookupNamespaceURI(''), 'http://other.com' );

    # non-existent namespaces
    ok ( $root->lookupNamespaceURI('foo'), undef );
    ok ( $root->lookupNamespacePrefix('foo'), undef );
    ok ( $root->getAttribute('xmlns:foo'), undef );

    # changing namespace declaration URI and prefix
    ok ( $root->setNamespaceDeclURI('yyy', 'http://changed.com') );
    ok ( $root->getAttribute('xmlns:yyy'), 'http://changed.com');
    ok ( $root->lookupNamespaceURI('yyy'), 'http://changed.com');
    eval { $root->setNamespaceDeclPrefix('yyy','xxx'); };
    ok ( $@ );  # prefix occupied
    eval { $root->setNamespaceDeclPrefix('yyy',''); };
    ok ( $@ );  # prefix occupied
    ok ( $root->setNamespaceDeclPrefix('yyy', 'zzz') );
    ok ( $root->lookupNamespaceURI('yyy'), undef );
    ok ( $root->lookupNamespaceURI('zzz'), 'http://changed.com' );
    ok ( $root->setNamespaceDeclURI('zzz',undef ) ); 
    ok ( $root->lookupNamespaceURI('zzz'), undef );
    $strnode = $root->toString();
    ok ( $strnode !~ /xmlns:zzz/ );

    # changing the default namespace declaration
    ok ( $root->setNamespaceDeclURI('','http://test') );	
    ok ( $root->lookupNamespaceURI(''), 'http://test' );
    ok ( $root->getNamespaceURI(), 'http://test' );

    # changing prefix of the default ns declaration
    ok ( $root->setNamespaceDeclPrefix('','foo') );	
    ok ( $root->lookupNamespaceURI(''), undef );
    ok ( $root->lookupNamespaceURI('foo'), 'http://test' );
    ok ( $root->getNamespaceURI(),  'http://test' );
    ok ( $root->prefix(),  'foo' );

    # turning a ns declaration to a default ns declaration
    ok ( $root->setNamespaceDeclPrefix('foo','') );	
    ok ( $root->lookupNamespaceURI('foo'), undef );
    ok ( $root->lookupNamespaceURI(''), 'http://test' );
    ok ( $root->lookupNamespaceURI(undef), 'http://test' );
    ok ( $root->getNamespaceURI(),  'http://test' );
    ok ( $root->prefix(),  undef );

    # removing the default ns declaration
    ok ( $root->setNamespaceDeclURI('',undef) );
    ok ( $root->lookupNamespaceURI(''), undef );
    ok ( $root->getNamespaceURI(), undef );

    $strnode = $root->toString();
    ok ( $strnode !~ /xmlns=/ );

    # namespaced attributes
    $root->setAttribute('xxx:attr', 'value');
    ok ( $root->getAttributeNode('xxx:attr') );
    ok ( $root->getAttribute('xxx:attr'), 'value' );
    print $root->toString(1),"\n";
   ok ( $root->getAttributeNodeNS('http://example.com','attr') );
   ok ( $root->getAttributeNS('http://example.com','attr'), 'value' );
   ok ( $root->getAttributeNode('xxx:attr')->getNamespaceURI(), 'http://example.com');

    # removing other xmlns declarations
    $root->addNewChild('http://example.com', 'xxx:foo');
    ok( $root->setNamespaceDeclURI('xxx',undef) );	
    ok ( $root->lookupNamespaceURI('xxx'), undef );
    ok ( $root->getNamespaceURI(), undef );
    ok ( $root->firstChild->getNamespaceURI(), undef );
    ok ( $root->prefix(),  undef );
    ok ( $root->firstChild->prefix(),  undef );


    print $root->toString(1),"\n";
    # check namespaced attributes
    ok ( $root->getAttributeNode('xxx:attr'), undef );
   ok ( $root->getAttributeNodeNS('http://example.com', 'attr'), undef );
   ok ( $root->getAttributeNode('attr') );
    ok ( $root->getAttribute('attr'), 'value' );
   ok ( $root->getAttributeNodeNS(undef,'attr') );
   ok ( $root->getAttributeNS(undef,'attr'), 'value' );
    ok ( $root->getAttributeNode('attr')->getNamespaceURI(), undef);


    $strnode = $root->toString();
    ok ( $strnode !~ /xmlns=/ );
    ok ( $strnode !~ /xmlns:xxx=/ );
    ok ( $strnode =~ /<foo/ );
    
    ok ( $root->setNamespaceDeclPrefix('xxx',undef) );

    ok ( $doc->findnodes('/document/foo')->size(), 1 );
    ok ( $doc->findnodes('/document[foo]')->size(), 1 );
    ok ( $doc->findnodes('/document[*]')->size(), 1 );
   ok ( $doc->findnodes('/document[@attr and foo]')->size(), 1 );
   ok ( $doc->findvalue('/document/@attr'), 'value' );

    $xp = XML::LibXML::XPathContext->new($doc);
    ok ( $xp->findnodes('/document/foo')->size(), 1 );
    ok ( $xp->findnodes('/document[foo]')->size(), 1 );
    ok ( $xp->findnodes('/document[*]')->size(), 1 );

   ok ( $xp->findnodes('/document[@attr and foo]')->size(), 1 );
   ok ( $xp->findvalue('/document/@attr'), 'value' );

    ok ( $root->firstChild->prefix(),  undef );
}

print "# 9. namespace reconciliation\n";
{
	my $doc = XML::LibXML->createDocument( 'http://default', 'root' );
	my $root = $doc->documentElement;
	$root->setNamespace( 'http://children', 'child', 0 );
	
	$root->appendChild( my $n = $doc->createElementNS( 'http://default', 'branch' ));
	# appending an element in the same namespace will
	# strip its declaration
	ok( !defined($n->getAttribute( 'xmlns' )) );
	
	$n->appendChild( my $a = $doc->createElementNS( 'http://children', 'child:a' ));
	$n->appendChild( my $b = $doc->createElementNS( 'http://children', 'child:b' ));
	
	$n->appendChild( my $c = $doc->createElementNS( 'http://children', 'child:c' ));
	# appending $c strips the declaration
	ok( !defined($c->getAttribute('xmlns:child')) );

	# add another prefix for children
	$c->setAttribute( 'xmlns:foo', 'http://children' );
	ok( $c->getAttribute( 'xmlns:foo' ), 'http://children' );

	$n->appendChild( my $d = $doc->createElementNS( 'http://other', 'branch' ));
	# appending an element with a new default namespace
	# will leave it declared
	ok( $d->getAttribute( 'xmlns' ), 'http://other' );

	my $doca = XML::LibXML->createDocument( 'http://default/', 'root' );
	$doca->adoptNode( $a );
	$doca->adoptNode( $b );
	$doca->documentElement->appendChild( $a );
	$doca->documentElement->appendChild( $b );

	# Because the child namespace isn't defined in $doca
	# it should get declared on both child nodes $a and $b
	ok( $a->getAttribute( 'xmlns:child' ), 'http://children' );
	ok( $b->getAttribute( 'xmlns:child' ), 'http://children' );

	$doca = XML::LibXML->createDocument( 'http://children', 'child:root' );
	$doca->adoptNode( $a );
	$doca->documentElement->appendChild( $a );

	# $doca declares the child namespace, so the declaration
	# should now get stripped from $a
	ok( !defined($a->getAttribute( 'xmlns:child' )) );

	$doca->documentElement->removeChild( $a );

	# $a should now have its namespace re-declared
	ok( $a->getAttribute( 'xmlns:child' ), 'http://children' );

	$doca->documentElement->appendChild( $a );

	# $doca declares the child namespace, so the declaration
	# should now get stripped from $a
	ok( !defined($a->getAttribute( 'xmlns:child' )) );


	$doc = XML::LibXML::Document->new;
	$n = $doc->createElement( 'didl' );
	$n->setAttribute( "xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance" );
	
	$a = $doc->createElement( 'dc' );
	$a->setAttribute( "xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance" );
	$a->setAttribute( "xsi:schemaLocation"=>"http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives
.org/OAI/2.0/oai_dc.xsd" );
	
	$n->appendChild( $a );
	
	# the declaration for xsi should be stripped
	ok( !defined($a->getAttribute( 'xmlns:xsi' )) );
	
	$n->removeChild( $a );
	
	# should be a new declaration for xsi in $a
	ok( $a->getAttribute( 'xmlns:xsi' ), 'http://www.w3.org/2001/XMLSchema-instance' );
	
	$b = $doc->createElement( 'foo' );
	$b->setAttribute( 'xsi:bar', 'bar' );
	$n->appendChild( $b );
	$n->removeChild( $b );
	
	# a prefix without a namespace can't be reliably compared,
	# so $b doesn't acquire a declaration from $n!
	ok( !defined($b->getAttribute( 'xmlns:xsi' )) );

	# tests for reconciliation during setAttributeNodeNS
	my $attr = $doca->createAttributeNS('http://children',
					    'child:attr','value');
	ok($attr);
	my $child= $doca->documentElement->firstChild;
	ok($child);
	$child->setAttributeNodeNS($attr);
	ok ( !defined($child->getAttribute( 'xmlns:child' )) );

	# due to libxml2 limitation, XML::LibXML declares the namespace
	# on the root element
	$attr = $doca->createAttributeNS('http://other','other:attr','value');
	ok($attr);
	$child->setAttributeNodeNS($attr);
	#
	ok ( !defined($child->getAttribute( 'xmlns:other' )) );
	ok ( defined($doca->documentElement->getAttribute( 'xmlns:other' )) );
}

print "# 10. xml namespace\n";
{
  my $docOne = XML::LibXML->new->parse_string('<foo><inc xml:id="test"/></foo>');
  my $docTwo = XML::LibXML->new->parse_string('<bar><urgh xml:id="foo"/></bar>');

  my $inc = $docOne->getElementById('test');
  my $rep = $docTwo->getElementById('foo');
  $inc->parentNode->replaceChild($rep, $inc);
  ok($inc->getAttributeNS('http://www.w3.org/XML/1998/namespace','id'),'test');
  ok($inc->isSameNode($docOne->getElementById('test')));
}