| 1 |
# -*- cperl -*- |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
# Should be 137. |
| 7 |
use Test::More tests => 137; |
| 8 |
|
| 9 |
use XML::LibXML; |
| 10 |
use XML::LibXML::Common qw(:libxml); |
| 11 |
|
| 12 |
my $parser = XML::LibXML->new(); |
| 13 |
|
| 14 |
my $xml1 = <<EOX; |
| 15 |
<a xmlns:b="http://whatever" |
| 16 |
><x b:href="out.xml" |
| 17 |
/><b:c/></a> |
| 18 |
EOX |
| 19 |
|
| 20 |
my $xml2 = <<EOX; |
| 21 |
<a xmlns:b="http://whatever" xmlns:c="http://kungfoo" |
| 22 |
><x b:href="out.xml" |
| 23 |
/><b:c/><c:b/></a> |
| 24 |
EOX |
| 25 |
|
| 26 |
my $xml3 = <<EOX; |
| 27 |
<a xmlns:b="http://whatever"> |
| 28 |
<x b:href="out.xml"/> |
| 29 |
<x> |
| 30 |
<c:b xmlns:c="http://kungfoo"> |
| 31 |
<c:d/> |
| 32 |
</c:b> |
| 33 |
</x> |
| 34 |
<x> |
| 35 |
<c:b xmlns:c="http://foobar"> |
| 36 |
<c:d/> |
| 37 |
</c:b> |
| 38 |
</x> |
| 39 |
</a> |
| 40 |
EOX |
| 41 |
|
| 42 |
print "# 1. single namespace \n"; |
| 43 |
|
| 44 |
{ |
| 45 |
my $doc1 = $parser->parse_string( $xml1 ); |
| 46 |
my $elem = $doc1->documentElement; |
| 47 |
# TEST |
| 48 |
is($elem->lookupNamespaceURI( "b" ), "http://whatever", ' TODO : Add test name' ); |
| 49 |
my @cn = $elem->childNodes; |
| 50 |
# TEST |
| 51 |
is($cn[0]->lookupNamespaceURI( "b" ), "http://whatever", ' TODO : Add test name' ); |
| 52 |
# TEST |
| 53 |
is($cn[1]->namespaceURI, "http://whatever", ' TODO : Add test name' ); |
| 54 |
} |
| 55 |
|
| 56 |
print "# 2. multiple namespaces \n"; |
| 57 |
|
| 58 |
{ |
| 59 |
my $doc2 = $parser->parse_string( $xml2 ); |
| 60 |
|
| 61 |
my $elem = $doc2->documentElement; |
| 62 |
# TEST |
| 63 |
is($elem->lookupNamespaceURI( "b" ), "http://whatever", ' TODO : Add test name'); |
| 64 |
# TEST |
| 65 |
is($elem->lookupNamespaceURI( "c" ), "http://kungfoo", ' TODO : Add test name'); |
| 66 |
my @cn = $elem->childNodes; |
| 67 |
|
| 68 |
# TEST |
| 69 |
|
| 70 |
is($cn[0]->lookupNamespaceURI( "b" ), "http://whatever", ' TODO : Add test name' ); |
| 71 |
# TEST |
| 72 |
is($cn[0]->lookupNamespaceURI( "c" ), "http://kungfoo", ' TODO : Add test name'); |
| 73 |
|
| 74 |
# TEST |
| 75 |
|
| 76 |
is($cn[1]->namespaceURI, "http://whatever", ' TODO : Add test name' ); |
| 77 |
# TEST |
| 78 |
is($cn[2]->namespaceURI, "http://kungfoo", ' TODO : Add test name' ); |
| 79 |
} |
| 80 |
|
| 81 |
print "# 3. nested names \n"; |
| 82 |
|
| 83 |
{ |
| 84 |
my $doc3 = $parser->parse_string( $xml3 ); |
| 85 |
my $elem = $doc3->documentElement; |
| 86 |
my @cn = $elem->childNodes; |
| 87 |
my @xs = grep { $_->nodeType == XML_ELEMENT_NODE } @cn; |
| 88 |
|
| 89 |
my @x1 = $xs[1]->childNodes; my @x2 = $xs[2]->childNodes; |
| 90 |
|
| 91 |
# TEST |
| 92 |
|
| 93 |
is( $x1[1]->namespaceURI , "http://kungfoo", ' TODO : Add test name' ); |
| 94 |
# TEST |
| 95 |
is( $x2[1]->namespaceURI , "http://foobar", ' TODO : Add test name' ); |
| 96 |
|
| 97 |
# namespace scopeing |
| 98 |
# TEST |
| 99 |
ok( !defined($elem->lookupNamespacePrefix( "http://kungfoo" )), ' TODO : Add test name' ); |
| 100 |
# TEST |
| 101 |
ok( !defined($elem->lookupNamespacePrefix( "http://foobar" )), ' TODO : Add test name' ); |
| 102 |
} |
| 103 |
|
| 104 |
print "# 4. post creation namespace setting\n"; |
| 105 |
{ |
| 106 |
my $e1 = XML::LibXML::Element->new("foo"); |
| 107 |
my $e2 = XML::LibXML::Element->new("bar:foo"); |
| 108 |
my $e3 = XML::LibXML::Element->new("foo"); |
| 109 |
$e3->setAttribute( "kung", "foo" ); |
| 110 |
my $a = $e3->getAttributeNode("kung"); |
| 111 |
|
| 112 |
$e1->appendChild($e2); |
| 113 |
$e2->appendChild($e3); |
| 114 |
# TEST |
| 115 |
ok( $e2->setNamespace("http://kungfoo", "bar"), ' TODO : Add test name' ); |
| 116 |
# TEST |
| 117 |
ok( $a->setNamespace("http://kungfoo", "bar"), ' TODO : Add test name' ); |
| 118 |
# TEST |
| 119 |
is( $a->nodeName, "bar:kung", ' TODO : Add test name' ); |
| 120 |
} |
| 121 |
|
| 122 |
print "# 5. importing namespaces\n"; |
| 123 |
|
| 124 |
{ |
| 125 |
|
| 126 |
my $doca = XML::LibXML->createDocument; |
| 127 |
my $docb = XML::LibXML->new()->parse_string( <<EOX ); |
| 128 |
<x:a xmlns:x="http://foo.bar"><x:b/></x:a> |
| 129 |
EOX |
| 130 |
|
| 131 |
my $b = $docb->documentElement->firstChild; |
| 132 |
|
| 133 |
my $c = $doca->importNode( $b ); |
| 134 |
|
| 135 |
my @attra = $c->attributes; |
| 136 |
# TEST |
| 137 |
is( scalar(@attra), 1, ' TODO : Add test name' ); |
| 138 |
# TEST |
| 139 |
is( $attra[0]->nodeType, 18, ' TODO : Add test name' ); |
| 140 |
my $d = $doca->adoptNode($b); |
| 141 |
|
| 142 |
# TEST |
| 143 |
|
| 144 |
ok( $d->isSameNode( $b ), ' TODO : Add test name' ); |
| 145 |
my @attrb = $d->attributes; |
| 146 |
# TEST |
| 147 |
is( scalar(@attrb), 1, ' TODO : Add test name' ); |
| 148 |
# TEST |
| 149 |
is( $attrb[0]->nodeType, 18, ' TODO : Add test name' ); |
| 150 |
} |
| 151 |
|
| 152 |
print "# 6. lossless setting of namespaces with setAttribute\n"; |
| 153 |
# reported by Kurt George Gjerde |
| 154 |
{ |
| 155 |
my $doc = XML::LibXML->createDocument; |
| 156 |
my $root = $doc->createElementNS('http://example.com', 'document'); |
| 157 |
$root->setAttribute('xmlns:xxx', 'http://example.com'); |
| 158 |
$root->setAttribute('xmlns:yyy', 'http://yonder.com'); |
| 159 |
$doc->setDocumentElement( $root ); |
| 160 |
|
| 161 |
my $strnode = $root->toString(); |
| 162 |
# TEST |
| 163 |
ok ( $strnode =~ /xmlns:xxx/ and $strnode =~ /xmlns=/, ' TODO : Add test name' ); |
| 164 |
} |
| 165 |
|
| 166 |
print "# 7. namespaced attributes\n"; |
| 167 |
{ |
| 168 |
my $doc = XML::LibXML->new->parse_string(<<'EOF'); |
| 169 |
<test xmlns:xxx="http://example.com"/> |
| 170 |
EOF |
| 171 |
my $root = $doc->getDocumentElement(); |
| 172 |
# namespaced attributes |
| 173 |
$root->setAttribute('xxx:attr', 'value'); |
| 174 |
# TEST |
| 175 |
ok ( $root->getAttributeNode('xxx:attr'), ' TODO : Add test name' ); |
| 176 |
# TEST |
| 177 |
is ( $root->getAttribute('xxx:attr'), 'value', ' TODO : Add test name' ); |
| 178 |
print $root->toString(1),"\n"; |
| 179 |
# TEST |
| 180 |
ok ( $root->getAttributeNodeNS('http://example.com','attr'), ' TODO : Add test name' ); |
| 181 |
# TEST |
| 182 |
is ( $root->getAttributeNS('http://example.com','attr'), 'value', ' TODO : Add test name' ); |
| 183 |
# TEST |
| 184 |
is ( $root->getAttributeNode('xxx:attr')->getNamespaceURI(), 'http://example.com', ' TODO : Add test name'); |
| 185 |
|
| 186 |
#change encoding to UTF-8 and retest |
| 187 |
$doc->setEncoding('UTF-8'); |
| 188 |
# namespaced attributes |
| 189 |
$root->setAttribute('xxx:attr', 'value'); |
| 190 |
# TEST |
| 191 |
ok ( $root->getAttributeNode('xxx:attr'), ' TODO : Add test name' ); |
| 192 |
# TEST |
| 193 |
is ( $root->getAttribute('xxx:attr'), 'value', ' TODO : Add test name' ); |
| 194 |
print $root->toString(1),"\n"; |
| 195 |
# TEST |
| 196 |
ok ( $root->getAttributeNodeNS('http://example.com','attr'), ' TODO : Add test name' ); |
| 197 |
# TEST |
| 198 |
is ( $root->getAttributeNS('http://example.com','attr'), 'value', ' TODO : Add test name' ); |
| 199 |
# TEST |
| 200 |
is ( $root->getAttributeNode('xxx:attr')->getNamespaceURI(), |
| 201 |
'http://example.com', ' TODO : Add test name'); |
| 202 |
} |
| 203 |
|
| 204 |
print "# 8. changing namespace declarations\n"; |
| 205 |
{ |
| 206 |
my $xmlns = 'http://www.w3.org/2000/xmlns/'; |
| 207 |
|
| 208 |
my $doc = XML::LibXML->createDocument; |
| 209 |
my $root = $doc->createElementNS('http://example.com', 'document'); |
| 210 |
$root->setAttributeNS($xmlns, 'xmlns:xxx', 'http://example.com'); |
| 211 |
$root->setAttribute('xmlns:yyy', 'http://yonder.com'); |
| 212 |
$doc->setDocumentElement( $root ); |
| 213 |
|
| 214 |
# can we get the namespaces ? |
| 215 |
# TEST |
| 216 |
is ( $root->getAttribute('xmlns:xxx'), 'http://example.com', ' TODO : Add test name'); |
| 217 |
# TEST |
| 218 |
is ( $root->getAttributeNS($xmlns,'xmlns'), 'http://example.com', ' TODO : Add test name' ); |
| 219 |
# TEST |
| 220 |
is ( $root->getAttribute('xmlns:yyy'), 'http://yonder.com', ' TODO : Add test name'); |
| 221 |
# TEST |
| 222 |
is ( $root->lookupNamespacePrefix('http://yonder.com'), 'yyy', ' TODO : Add test name'); |
| 223 |
# TEST |
| 224 |
is ( $root->lookupNamespaceURI('yyy'), 'http://yonder.com', ' TODO : Add test name'); |
| 225 |
|
| 226 |
# can we change the namespaces ? |
| 227 |
# TEST |
| 228 |
ok ( $root->setAttribute('xmlns:yyy', 'http://newyonder.com'), ' TODO : Add test name' ); |
| 229 |
# TEST |
| 230 |
is ( $root->getAttribute('xmlns:yyy'), 'http://newyonder.com', ' TODO : Add test name'); |
| 231 |
# TEST |
| 232 |
is ( $root->lookupNamespacePrefix('http://newyonder.com'), 'yyy', ' TODO : Add test name'); |
| 233 |
# TEST |
| 234 |
is ( $root->lookupNamespaceURI('yyy'), 'http://newyonder.com', ' TODO : Add test name'); |
| 235 |
|
| 236 |
# can we change the default namespace ? |
| 237 |
$root->setAttribute('xmlns', 'http://other.com' ); |
| 238 |
# TEST |
| 239 |
is ( $root->getAttribute('xmlns'), 'http://other.com', ' TODO : Add test name' ); |
| 240 |
# TEST |
| 241 |
is ( $root->lookupNamespacePrefix('http://other.com'), "", ' TODO : Add test name' ); |
| 242 |
# TEST |
| 243 |
is ( $root->lookupNamespaceURI(''), 'http://other.com', ' TODO : Add test name' ); |
| 244 |
|
| 245 |
# non-existent namespaces |
| 246 |
# TEST |
| 247 |
is ( $root->lookupNamespaceURI('foo'), undef, ' TODO : Add test name' ); |
| 248 |
# TEST |
| 249 |
is ( $root->lookupNamespacePrefix('foo'), undef, ' TODO : Add test name' ); |
| 250 |
# TEST |
| 251 |
is ( $root->getAttribute('xmlns:foo'), undef, ' TODO : Add test name' ); |
| 252 |
|
| 253 |
# changing namespace declaration URI and prefix |
| 254 |
# TEST |
| 255 |
ok ( $root->setNamespaceDeclURI('yyy', 'http://changed.com'), ' TODO : Add test name' ); |
| 256 |
# TEST |
| 257 |
is ( $root->getAttribute('xmlns:yyy'), 'http://changed.com', ' TODO : Add test name'); |
| 258 |
# TEST |
| 259 |
is ( $root->lookupNamespaceURI('yyy'), 'http://changed.com', ' TODO : Add test name'); |
| 260 |
eval { $root->setNamespaceDeclPrefix('yyy','xxx'); }; |
| 261 |
# TEST |
| 262 |
ok ( $@, ' TODO : Add test name' ); # prefix occupied |
| 263 |
eval { $root->setNamespaceDeclPrefix('yyy',''); }; |
| 264 |
# TEST |
| 265 |
ok ( $@, ' TODO : Add test name' ); # prefix occupied |
| 266 |
# TEST |
| 267 |
ok ( $root->setNamespaceDeclPrefix('yyy', 'zzz'), ' TODO : Add test name' ); |
| 268 |
# TEST |
| 269 |
is ( $root->lookupNamespaceURI('yyy'), undef, ' TODO : Add test name' ); |
| 270 |
# TEST |
| 271 |
is ( $root->lookupNamespaceURI('zzz'), 'http://changed.com', ' TODO : Add test name' ); |
| 272 |
# TEST |
| 273 |
ok ( $root->setNamespaceDeclURI('zzz',undef ), ' TODO : Add test name' ); |
| 274 |
# TEST |
| 275 |
is ( $root->lookupNamespaceURI('zzz'), undef, ' TODO : Add test name' ); |
| 276 |
|
| 277 |
my $strnode = $root->toString(); |
| 278 |
# TEST |
| 279 |
ok ( $strnode !~ /xmlns:zzz/, ' TODO : Add test name' ); |
| 280 |
|
| 281 |
# changing the default namespace declaration |
| 282 |
# TEST |
| 283 |
ok ( $root->setNamespaceDeclURI('','http://test'), ' TODO : Add test name' ); |
| 284 |
# TEST |
| 285 |
is ( $root->lookupNamespaceURI(''), 'http://test', ' TODO : Add test name' ); |
| 286 |
# TEST |
| 287 |
is ( $root->getNamespaceURI(), 'http://test', ' TODO : Add test name' ); |
| 288 |
|
| 289 |
# changing prefix of the default ns declaration |
| 290 |
# TEST |
| 291 |
ok ( $root->setNamespaceDeclPrefix('','foo'), ' TODO : Add test name' ); |
| 292 |
# TEST |
| 293 |
is ( $root->lookupNamespaceURI(''), undef, ' TODO : Add test name' ); |
| 294 |
# TEST |
| 295 |
is ( $root->lookupNamespaceURI('foo'), 'http://test', ' TODO : Add test name' ); |
| 296 |
# TEST |
| 297 |
is ( $root->getNamespaceURI(), 'http://test', ' TODO : Add test name' ); |
| 298 |
# TEST |
| 299 |
is ( $root->prefix(), 'foo', ' TODO : Add test name' ); |
| 300 |
|
| 301 |
# turning a ns declaration to a default ns declaration |
| 302 |
# TEST |
| 303 |
ok ( $root->setNamespaceDeclPrefix('foo',''), ' TODO : Add test name' ); |
| 304 |
# TEST |
| 305 |
is ( $root->lookupNamespaceURI('foo'), undef, ' TODO : Add test name' ); |
| 306 |
# TEST |
| 307 |
is ( $root->lookupNamespaceURI(''), 'http://test', ' TODO : Add test name' ); |
| 308 |
# TEST |
| 309 |
is ( $root->lookupNamespaceURI(undef), 'http://test', ' TODO : Add test name' ); |
| 310 |
# TEST |
| 311 |
is ( $root->getNamespaceURI(), 'http://test', ' TODO : Add test name' ); |
| 312 |
# TEST |
| 313 |
is ( $root->prefix(), undef, ' TODO : Add test name' ); |
| 314 |
|
| 315 |
# removing the default ns declaration |
| 316 |
# TEST |
| 317 |
ok ( $root->setNamespaceDeclURI('',undef), ' TODO : Add test name' ); |
| 318 |
# TEST |
| 319 |
is ( $root->lookupNamespaceURI(''), undef, ' TODO : Add test name' ); |
| 320 |
# TEST |
| 321 |
is ( $root->getNamespaceURI(), undef, ' TODO : Add test name' ); |
| 322 |
|
| 323 |
$strnode = $root->toString(); |
| 324 |
# TEST |
| 325 |
ok ( $strnode !~ /xmlns=/, ' TODO : Add test name' ); |
| 326 |
|
| 327 |
# namespaced attributes |
| 328 |
$root->setAttribute('xxx:attr', 'value'); |
| 329 |
# TEST |
| 330 |
ok ( $root->getAttributeNode('xxx:attr'), ' TODO : Add test name' ); |
| 331 |
# TEST |
| 332 |
is ( $root->getAttribute('xxx:attr'), 'value', ' TODO : Add test name' ); |
| 333 |
print $root->toString(1),"\n"; |
| 334 |
# TEST |
| 335 |
ok ( $root->getAttributeNodeNS('http://example.com','attr'), ' TODO : Add test name' ); |
| 336 |
# TEST |
| 337 |
is ( $root->getAttributeNS('http://example.com','attr'), 'value', ' TODO : Add test name' ); |
| 338 |
# TEST |
| 339 |
is ( $root->getAttributeNode('xxx:attr')->getNamespaceURI(), 'http://example.com', ' TODO : Add test name'); |
| 340 |
|
| 341 |
# removing other xmlns declarations |
| 342 |
$root->addNewChild('http://example.com', 'xxx:foo'); |
| 343 |
# TEST |
| 344 |
ok( $root->setNamespaceDeclURI('xxx',undef), ' TODO : Add test name' ); |
| 345 |
# TEST |
| 346 |
is ( $root->lookupNamespaceURI('xxx'), undef, ' TODO : Add test name' ); |
| 347 |
# TEST |
| 348 |
is ( $root->getNamespaceURI(), undef, ' TODO : Add test name' ); |
| 349 |
# TEST |
| 350 |
is ( $root->firstChild->getNamespaceURI(), undef, ' TODO : Add test name' ); |
| 351 |
# TEST |
| 352 |
is ( $root->prefix(), undef, ' TODO : Add test name' ); |
| 353 |
# TEST |
| 354 |
is ( $root->firstChild->prefix(), undef, ' TODO : Add test name' ); |
| 355 |
|
| 356 |
|
| 357 |
print $root->toString(1),"\n"; |
| 358 |
# check namespaced attributes |
| 359 |
# TEST |
| 360 |
is ( $root->getAttributeNode('xxx:attr'), undef, ' TODO : Add test name' ); |
| 361 |
# TEST |
| 362 |
is ( $root->getAttributeNodeNS('http://example.com', 'attr'), undef, ' TODO : Add test name' ); |
| 363 |
# TEST |
| 364 |
ok ( $root->getAttributeNode('attr'), ' TODO : Add test name' ); |
| 365 |
# TEST |
| 366 |
is ( $root->getAttribute('attr'), 'value', ' TODO : Add test name' ); |
| 367 |
# TEST |
| 368 |
ok ( $root->getAttributeNodeNS(undef,'attr'), ' TODO : Add test name' ); |
| 369 |
# TEST |
| 370 |
is ( $root->getAttributeNS(undef,'attr'), 'value', ' TODO : Add test name' ); |
| 371 |
# TEST |
| 372 |
is ( $root->getAttributeNode('attr')->getNamespaceURI(), undef, ' TODO : Add test name'); |
| 373 |
|
| 374 |
|
| 375 |
$strnode = $root->toString(); |
| 376 |
# TEST |
| 377 |
ok ( $strnode !~ /xmlns=/, ' TODO : Add test name' ); |
| 378 |
# TEST |
| 379 |
ok ( $strnode !~ /xmlns:xxx=/, ' TODO : Add test name' ); |
| 380 |
# TEST |
| 381 |
ok ( $strnode =~ /<foo/, ' TODO : Add test name' ); |
| 382 |
|
| 383 |
# TEST |
| 384 |
|
| 385 |
ok ( $root->setNamespaceDeclPrefix('xxx',undef), ' TODO : Add test name' ); |
| 386 |
|
| 387 |
# TEST |
| 388 |
|
| 389 |
is ( $doc->findnodes('/document/foo')->size(), 1, ' TODO : Add test name' ); |
| 390 |
# TEST |
| 391 |
is ( $doc->findnodes('/document[foo]')->size(), 1, ' TODO : Add test name' ); |
| 392 |
# TEST |
| 393 |
is ( $doc->findnodes('/document[*]')->size(), 1, ' TODO : Add test name' ); |
| 394 |
# TEST |
| 395 |
is ( $doc->findnodes('/document[@attr and foo]')->size(), 1, ' TODO : Add test name' ); |
| 396 |
# TEST |
| 397 |
is ( $doc->findvalue('/document/@attr'), 'value', ' TODO : Add test name' ); |
| 398 |
|
| 399 |
my $xp = XML::LibXML::XPathContext->new($doc); |
| 400 |
# TEST |
| 401 |
is ( $xp->findnodes('/document/foo')->size(), 1, ' TODO : Add test name' ); |
| 402 |
# TEST |
| 403 |
is ( $xp->findnodes('/document[foo]')->size(), 1, ' TODO : Add test name' ); |
| 404 |
# TEST |
| 405 |
is ( $xp->findnodes('/document[*]')->size(), 1, ' TODO : Add test name' ); |
| 406 |
|
| 407 |
# TEST |
| 408 |
|
| 409 |
is ( $xp->findnodes('/document[@attr and foo]')->size(), 1, ' TODO : Add test name' ); |
| 410 |
# TEST |
| 411 |
is ( $xp->findvalue('/document/@attr'), 'value', ' TODO : Add test name' ); |
| 412 |
|
| 413 |
# TEST |
| 414 |
|
| 415 |
is ( $root->firstChild->prefix(), undef, ' TODO : Add test name' ); |
| 416 |
} |
| 417 |
|
| 418 |
print "# 9. namespace reconciliation\n"; |
| 419 |
{ |
| 420 |
my $doc = XML::LibXML->createDocument( 'http://default', 'root' ); |
| 421 |
my $root = $doc->documentElement; |
| 422 |
$root->setNamespace( 'http://children', 'child', 0 ); |
| 423 |
|
| 424 |
$root->appendChild( my $n = $doc->createElementNS( 'http://default', 'branch' )); |
| 425 |
# appending an element in the same namespace will |
| 426 |
# strip its declaration |
| 427 |
# TEST |
| 428 |
ok( !defined($n->getAttribute( 'xmlns' )), ' TODO : Add test name' ); |
| 429 |
|
| 430 |
$n->appendChild( my $a = $doc->createElementNS( 'http://children', 'child:a' )); |
| 431 |
$n->appendChild( my $b = $doc->createElementNS( 'http://children', 'child:b' )); |
| 432 |
|
| 433 |
$n->appendChild( my $c = $doc->createElementNS( 'http://children', 'child:c' )); |
| 434 |
# appending $c strips the declaration |
| 435 |
# TEST |
| 436 |
ok( !defined($c->getAttribute('xmlns:child')), ' TODO : Add test name' ); |
| 437 |
|
| 438 |
# add another prefix for children |
| 439 |
$c->setAttribute( 'xmlns:foo', 'http://children' ); |
| 440 |
# TEST |
| 441 |
is( $c->getAttribute( 'xmlns:foo' ), 'http://children', ' TODO : Add test name' ); |
| 442 |
|
| 443 |
$n->appendChild( my $d = $doc->createElementNS( 'http://other', 'branch' )); |
| 444 |
# appending an element with a new default namespace |
| 445 |
# will leave it declared |
| 446 |
# TEST |
| 447 |
is( $d->getAttribute( 'xmlns' ), 'http://other', ' TODO : Add test name' ); |
| 448 |
|
| 449 |
my $doca = XML::LibXML->createDocument( 'http://default/', 'root' ); |
| 450 |
$doca->adoptNode( $a ); |
| 451 |
$doca->adoptNode( $b ); |
| 452 |
$doca->documentElement->appendChild( $a ); |
| 453 |
$doca->documentElement->appendChild( $b ); |
| 454 |
|
| 455 |
# Because the child namespace isn't defined in $doca |
| 456 |
# it should get declared on both child nodes $a and $b |
| 457 |
# TEST |
| 458 |
is( $a->getAttribute( 'xmlns:child' ), 'http://children', ' TODO : Add test name' ); |
| 459 |
# TEST |
| 460 |
is( $b->getAttribute( 'xmlns:child' ), 'http://children', ' TODO : Add test name' ); |
| 461 |
|
| 462 |
$doca = XML::LibXML->createDocument( 'http://children', 'child:root' ); |
| 463 |
$doca->adoptNode( $a ); |
| 464 |
$doca->documentElement->appendChild( $a ); |
| 465 |
|
| 466 |
# $doca declares the child namespace, so the declaration |
| 467 |
# should now get stripped from $a |
| 468 |
# TEST |
| 469 |
ok( !defined($a->getAttribute( 'xmlns:child' )), ' TODO : Add test name' ); |
| 470 |
|
| 471 |
$doca->documentElement->removeChild( $a ); |
| 472 |
|
| 473 |
# $a should now have its namespace re-declared |
| 474 |
# TEST |
| 475 |
is( $a->getAttribute( 'xmlns:child' ), 'http://children', ' TODO : Add test name' ); |
| 476 |
|
| 477 |
$doca->documentElement->appendChild( $a ); |
| 478 |
|
| 479 |
# $doca declares the child namespace, so the declaration |
| 480 |
# should now get stripped from $a |
| 481 |
# TEST |
| 482 |
ok( !defined($a->getAttribute( 'xmlns:child' )), ' TODO : Add test name' ); |
| 483 |
|
| 484 |
|
| 485 |
$doc = XML::LibXML::Document->new; |
| 486 |
$n = $doc->createElement( 'didl' ); |
| 487 |
$n->setAttribute( "xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance" ); |
| 488 |
|
| 489 |
$a = $doc->createElement( 'dc' ); |
| 490 |
$a->setAttribute( "xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance" ); |
| 491 |
$a->setAttribute( "xsi:schemaLocation"=>"http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives |
| 492 |
.org/OAI/2.0/oai_dc.xsd" ); |
| 493 |
|
| 494 |
$n->appendChild( $a ); |
| 495 |
|
| 496 |
# the declaration for xsi should be stripped |
| 497 |
# TEST |
| 498 |
ok( !defined($a->getAttribute( 'xmlns:xsi' )), ' TODO : Add test name' ); |
| 499 |
|
| 500 |
$n->removeChild( $a ); |
| 501 |
|
| 502 |
# should be a new declaration for xsi in $a |
| 503 |
# TEST |
| 504 |
is( $a->getAttribute( 'xmlns:xsi' ), 'http://www.w3.org/2001/XMLSchema-instance', ' TODO : Add test name' ); |
| 505 |
|
| 506 |
$b = $doc->createElement( 'foo' ); |
| 507 |
$b->setAttribute( 'xsi:bar', 'bar' ); |
| 508 |
$n->appendChild( $b ); |
| 509 |
$n->removeChild( $b ); |
| 510 |
|
| 511 |
# a prefix without a namespace can't be reliably compared, |
| 512 |
# so $b doesn't acquire a declaration from $n! |
| 513 |
# TEST |
| 514 |
ok( !defined($b->getAttribute( 'xmlns:xsi' )), ' TODO : Add test name' ); |
| 515 |
|
| 516 |
# tests for reconciliation during setAttributeNodeNS |
| 517 |
my $attr = $doca->createAttributeNS( |
| 518 |
'http://children', 'child:attr','value' |
| 519 |
); |
| 520 |
# TEST |
| 521 |
ok($attr, ' TODO : Add test name'); |
| 522 |
my $child= $doca->documentElement->firstChild; |
| 523 |
# TEST |
| 524 |
ok($child, ' TODO : Add test name'); |
| 525 |
$child->setAttributeNodeNS($attr); |
| 526 |
# TEST |
| 527 |
ok ( !defined($child->getAttribute( 'xmlns:child' )), ' TODO : Add test name' ); |
| 528 |
|
| 529 |
# due to libxml2 limitation, XML::LibXML declares the namespace |
| 530 |
# on the root element |
| 531 |
$attr = $doca->createAttributeNS('http://other','other:attr','value'); |
| 532 |
# TEST |
| 533 |
ok($attr, ' TODO : Add test name'); |
| 534 |
$child->setAttributeNodeNS($attr); |
| 535 |
# |
| 536 |
# TEST |
| 537 |
ok ( !defined($child->getAttribute( 'xmlns:other' )), ' TODO : Add test name' ); |
| 538 |
# TEST |
| 539 |
ok ( defined($doca->documentElement->getAttribute( 'xmlns:other' )), ' TODO : Add test name' ); |
| 540 |
} |
| 541 |
|
| 542 |
print "# 10. xml namespace\n"; |
| 543 |
{ |
| 544 |
my $docOne = XML::LibXML->new->parse_string( |
| 545 |
'<foo><inc xml:id="test"/></foo>' |
| 546 |
); |
| 547 |
my $docTwo = XML::LibXML->new->parse_string( |
| 548 |
'<bar><urgh xml:id="foo"/></bar>' |
| 549 |
); |
| 550 |
|
| 551 |
my $inc = $docOne->getElementById('test'); |
| 552 |
my $rep = $docTwo->getElementById('foo'); |
| 553 |
$inc->parentNode->replaceChild($rep, $inc); |
| 554 |
# TEST |
| 555 |
is($inc->getAttributeNS('http://www.w3.org/XML/1998/namespace','id'),'test', ' TODO : Add test name'); |
| 556 |
# TEST |
| 557 |
ok($inc->isSameNode($docOne->getElementById('test')), ' TODO : Add test name'); |
| 558 |
} |
| 559 |
|
| 560 |
print "# 11. empty namespace\n"; |
| 561 |
{ |
| 562 |
my $doc = XML::LibXML->load_xml(string => $xml1); |
| 563 |
my $node = $doc->find('/a/b:c')->[0]; |
| 564 |
|
| 565 |
# TEST |
| 566 |
ok($node->setNamespace(""), 'removing ns from elemenet'); |
| 567 |
# TEST |
| 568 |
is($node->prefix, undef, 'ns prefix removed from element'); |
| 569 |
# TEST |
| 570 |
is($node->namespaceURI, undef, 'ns removed from element'); |
| 571 |
# TEST |
| 572 |
is($node->getName, 'c', 'ns removed from element name'); |
| 573 |
|
| 574 |
my $attr = $doc->find('/a/x/@b:href')->[0]; |
| 575 |
|
| 576 |
# TEST |
| 577 |
ok($attr->setNamespace("", ""), 'removing ns from attr'); |
| 578 |
# TEST |
| 579 |
is($attr->prefix, undef, 'ns prefix removed from attr'); |
| 580 |
# TEST |
| 581 |
is($attr->namespaceURI, undef, 'ns removed from attr'); |
| 582 |
# TEST |
| 583 |
is($attr->getName, 'href', 'ns removed from attr name'); |
| 584 |
} |