File-Raw-XML
view release on metacpan or search on metacpan
xt/xpath-libxml.t view on Meta::CPAN
. '<r xmlns:p="urn:p" id="r0" xml:lang="en-GB">'
. '<a id="e1">1</a>'
. '<b><a id="e2">2</a><p:e>pe</p:e><!--cm--></b>'
. '<a>3</a>'
. '<q xmlns="urn:q"/>'
. '</r>';
my %NS = (p => 'urn:p', q => 'urn:q', x => 'http://www.w3.org/XML/1998/namespace');
my $mine = file_xml_decode($XML, id_attrs => ['id']);
my $lx = XML::LibXML->new(no_network => 1)->parse_string($XML);
my $ctx = XML::LibXML::XPathContext->new($lx);
$ctx->registerNs($_, $NS{$_}) for keys %NS;
# Every one of these is libxml2 or its Perl binding departing from XPath
# 1.0, checked digit by digit when it was written.
my %DECIDED = (
'/r/namespace::*' => [
'N:p=urn:p|N:xml=http://www.w3.org/XML/1998/namespace',
'XPath 5.4: an element has a namespace node for every namespace in '
. 'scope, including the xml prefix, which is implicitly declared. '
. "libxml2's own count() sees it; XML::LibXML's findnodes drops it.",
],
'/r/b/namespace::*' => [
'N:p=urn:p|N:xml=http://www.w3.org/XML/1998/namespace',
'the same, on a descendant',
],
'string(1 div 3)' => [
'0.3333333333333333',
'XPath 4.2: as many digits as are necessary to uniquely distinguish '
. 'the value from all other IEEE 754 numeric values. Fifteen threes '
. 'reads back as a different double; sixteen reads back as this one. '
. 'libxml2 renders with a fixed fifteen significant digits.',
],
'string(0.1 + 0.2)' => [
'0.30000000000000004',
'the same sentence: 0.1 + 0.2 is not the double 0.3, and printing it '
. 'as 0.3 loses which double it was',
],
'string(123456789012345678901234567890)' => [
'123456789012345680000000000000',
'XPath 4.2: the Number is never in exponential notation. libxml2 '
. 'gives 1.23456789012346e+29, which is not a Number at all.',
],
'string(0.000001)' => [
'0.000001',
'the same sentence, at the other end: libxml2 gives 1e-06',
],
'string(0.0000001)' => [
'0.0000001',
'and 1e-07',
],
'string(1e0 * 1)' => [
'REFUSED',
'XPath 3.7: Number is Digits ("." Digits?)? or "." Digits. There is '
. 'no exponent, so "1e0" is the Number 1 followed by the name "e0", '
. 'and a name where an operator belongs is an error. libxml2 accepts '
. 'an exponent as an extension.',
],
q{number('1e5')} => [
'NaN',
'XPath 4.4: a string is converted to a number when it is optional '
. 'whitespace, an optional minus sign, a Number and optional '
. 'whitespace, and to NaN otherwise. A Number has no exponent (3.7), '
. 'so "1e5" is not one. libxml2 converts it with strtod.',
],
);
sub render_mine {
my ($o) = @_;
my $r = ref $o;
return 'A:{' . $o->ns . '}' . $o->local . '=' . $o->value if $r eq 'File::Raw::XML::Attr';
return 'N:' . $o->prefix . '=' . $o->uri if $r eq 'File::Raw::XML::Namespace';
my $k = $o->kind;
return 'E:{' . $o->ns . '}' . $o->local if $k == 1;
return 'T:' . $o->text if $k == 2;
return 'C:' . $o->text if $k == 3;
return 'P:' . $o->local . '=' . $o->text if $k == 4;
return 'D:';
}
sub render_lx {
my ($o) = @_;
my $t = $o->nodeType;
return 'E:{' . ($o->namespaceURI // '') . '}' . $o->localname if $t == 1;
return 'A:{' . ($o->namespaceURI // '') . '}' . $o->localname . '=' . $o->value if $t == 2;
return 'T:' . $o->data if $t == 3 || $t == 4;
return 'C:' . $o->data if $t == 8;
return 'P:' . $o->nodeName . '=' . $o->textContent if $t == 7;
return 'N:' . ($o->declaredPrefix // '') . '=' . ($o->declaredURI // '') if $t == 18;
return 'D:' if $t == 9;
return "?$t";
}
my @EXPR = grep { !/^\s*(?:#|$)/ } map { chomp; $_ } <DATA>;
for my $e (@EXPR) {
my $is_set = eval { $ctx->find($e)->isa('XML::LibXML::NodeList') } ? 1 : 0;
my ($m, $l);
if ($is_set) {
eval { $m = join '|', map { render_mine($_) } $mine->xpath($e, ns => \%NS); 1 }
or $m = 'REFUSED';
eval { $l = join '|', map { render_lx($_) } $ctx->findnodes($e); 1 }
or $l = 'REFUSED';
} else {
eval { $m = scalar $mine->xpath("string($e)", ns => \%NS); 1 } or $m = 'REFUSED';
eval { $l = $ctx->findvalue("string($e)"); 1 } or $l = 'REFUSED';
}
$_ = '' for grep { !defined } ($m, $l);
my $decided = $DECIDED{$e};
if ($m eq $l) {
ok(!$decided, "$e: agrees with XML::LibXML")
or diag("this is listed as a decided disagreement and now agrees; "
. "remove it from \%DECIDED:\n $decided->[1]");
} elsif ($decided) {
is($m, $decided->[0], "$e: the decided disagreement, and this side is unchanged")
or diag($decided->[1]);
} else {
fail("$e: disagrees with XML::LibXML and is not decided");
diag(" mine: $m\n libxml: $l\n"
. " find the sentence of XPath 1.0 that decides this before changing either");
}
( run in 2.444 seconds using v1.01-cache-2.11-cpan-60884a8df76 )