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
|
#!/usr/bin/perl
use Test;
use XML::LibXML;
BEGIN {
if (XML::LibXML::LIBXML_VERSION() >= 20623) {
plan tests => 42;
}
else {
plan tests => 0;
print "# Skipping ID tests on libxml2 <= 2.6.23\n";
exit;
}
}
my $parser = XML::LibXML->new;
my $xml1 = <<'EOF';
<!DOCTYPE root [
<!ELEMENT root (root?)>
<!ATTLIST root id ID #REQUIRED
notid CDATA #IMPLIED
>
]>
<root id="foo" notid="x"/>
EOF
my $xml2 = <<'EOF';
<root2 xml:id="foo"/>
EOF
sub _debug {
my ($msg,$n)=@_;
print "$msg\t$$n\n'",(ref $n ? $n->toString : "NULL"),"'\n";
}
for my $do_validate (0..1) {
my ($n,$doc,$root,$at);
ok( $doc = $parser->parse_string($xml1) );
$root = $doc->getDocumentElement;
$n = $doc->getElementById('foo');
ok( $root->isSameNode( $n ) );
# old name
$n = $doc->getElementsById('foo');
ok( $root->isSameNode( $n ) );
$at = $n->getAttributeNode('id');
ok( $at );
ok( $at->isId );
$at = $root->getAttributeNode('notid');
ok( $at->isId == 0 );
# _debug("1: foo: ",$n);
$doc->getDocumentElement->setAttribute('id','bar');
ok( $doc->validate ) if $do_validate;
$n = $doc->getElementById('bar');
ok( $root->isSameNode( $n ) );
# _debug("1: bar: ",$n);
$n = $doc->getElementById('foo');
ok( !defined($n) );
# _debug("1: !foo: ",$n);
my $test = $doc->createElement('root');
$root->appendChild($test);
$test->setAttribute('id','new');
ok( $doc->validate ) if $do_validate;
$n = $doc->getElementById('new');
ok( $test->isSameNode( $n ) );
$at = $n->getAttributeNode('id');
ok( $at );
ok( $at->isId );
# _debug("1: new: ",$n);
}
{
my ($n,$doc,$root,$at);
ok( $doc = $parser->parse_string($xml2) );
$root = $doc->getDocumentElement;
$n = $doc->getElementById('foo');
ok( $root->isSameNode( $n ) );
# _debug("1: foo: ",$n);
$doc->getDocumentElement->setAttribute('xml:id','bar');
$n = $doc->getElementById('foo');
ok( !defined($n) );
# _debug("1: !foo: ",$n);
$n = $doc->getElementById('bar');
ok( $root->isSameNode( $n ) );
$at = $n->getAttributeNode('xml:id');
ok( $at );
ok( $at->isId );
$n->setAttribute('id','FOO');
ok( $at->isSameNode($n->getAttributeNode('xml:id')) );
$at = $n->getAttributeNode('id');
ok( $at );
ok( ! $at->isId );
$at = $n->getAttributeNodeNS('http://www.w3.org/XML/1998/namespace','id');
ok( $at );
ok( $at->isId );
# _debug("1: bar: ",$n);
$doc->getDocumentElement->setAttributeNS('http://www.w3.org/XML/1998/namespace','id','baz');
$n = $doc->getElementById('bar');
ok( !defined($n) );
# _debug("1: !bar: ",$n);
$n = $doc->getElementById('baz');
ok( $root->isSameNode( $n ) );
# _debug("1: baz: ",$n);
$at = $n->getAttributeNodeNS('http://www.w3.org/XML/1998/namespace','id');
ok( $at );
ok( $at->isId );
$doc->getDocumentElement->setAttributeNS('http://www.w3.org/XML/1998/namespace','xml:id','bag');
$n = $doc->getElementById('baz');
ok( !defined($n) );
# _debug("1: !baz: ",$n);
$n = $doc->getElementById('bag');
ok( $root->isSameNode( $n ) );
# _debug("1: bag: ",$n);
$n->removeAttribute('id');
ok( $root->toString, '<root2 xml:id="bag"/>' );
}
1;
|