summaryrefslogtreecommitdiff
path: root/ext/dom/tests/domxpath.phpt
blob: 82396b521816237d2148d51b21e021874e282922 (plain)
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
--TEST--
DOMXPath Tests
--SKIPIF--
<?php require_once('skipif.inc'); ?>
--FILE--
<?php
require_once("dom_test.inc");

function MyAverage($nodelist) {
	$count = 0;
	$val = 0;
	foreach ($nodelist AS $node) {
		$count++;
		$val += $node->textContent;
	}
	if ($val > 0) {
		return $val/$count;
	} else {
		return 0;
	}
}

$dom = new DOMDocument;
$dom->loadXML(b'<root xmlns="urn::default"><child>myval</child></root>');

$xpath = new DOMXPath($dom);

$xpath->registerPHPFunctions('MyAverage');
$xpath->registerNamespace("php", "http://php.net/xpath");

$xpath->registerNamespace("def", "urn::default");
$nodelist = $xpath->query("//def:child");
if ($node = $nodelist->item(0)) {
	print $node->textContent."\n";
}

$count = $xpath->evaluate("count(//def:child)");

var_dump($count);

$xpathdoc = $xpath->document;

var_dump($xpathdoc instanceof DOMDocument);

$root = $dom->documentElement;
$root->appendChild($dom->createElementNS("urn::default", "testnode", 3));
$root->appendChild($dom->createElementNS("urn::default", "testnode", 4));
$root->appendChild($dom->createElementNS("urn::default", "testnode", 4));
$root->appendChild($dom->createElementNS("urn::default", "testnode", 5));

$avg = $xpath->evaluate('number(php:function("MyAverage", //def:testnode))');
var_dump($avg);
?>
--EXPECT--
myval
float(1)
bool(true)
float(4)