File: xpath_query_context_in_scope_ns.phpt

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (98 lines) | stat: -rw-r--r-- 2,146 bytes parent folder | download
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
--TEST--
Test Dom\XPath::query() with registering a context node's in-scope namespaces
--EXTENSIONS--
dom
--FILE--
<?php

function dump($xpath, $query, $context) {
    echo "--- $query ---\n";
    $nodes = $xpath->query($query, $context);
    foreach ($nodes as $node) {
        echo "nodeName: ";
        var_dump($node->nodeName);
        echo "prefix: ";
        var_dump($node->prefix);
        echo "namespaceURI: ";
        var_dump($node->namespaceURI);
    }
    if (count($nodes) === 0) {
        echo "No nodes found\n";
    }
}

$dom = Dom\XMLDocument::createFromString(<<<XML
<a xmlns="urn:a" xmlns:d="urn:d">
    <b:b xmlns:b="urn:b">
        <c:c1 xmlns:c="urn:c1">
            <b:bar/>
            <c:c2 xmlns:c="urn:c2"/>
            <c:c3 xmlns:c="urn:c3"/>
            <c:c4 xmlns:c="urn:c1"/>
            <d:d/>
            <a/>
        </c:c1>
    </b:b>
</a>
XML);

$c1 = $dom->getElementsByTagNameNS('*', 'c1')->item(0);
$c2 = $dom->getElementsByTagNameNS('*', 'c1')->item(0);

$xpath = new Dom\XPath($dom);

echo "=== Tests with c1 ===\n\n";

dump($xpath, '//b:bar', $c1);
dump($xpath, '//c:c1', $c1);
dump($xpath, '//c:c2', $c1);
dump($xpath, '//c:c3', $c1);
dump($xpath, '//c:c4', $c1);
dump($xpath, '//d:d', $c1);
dump($xpath, '//a', $c1);

echo "\n=== Tests with c2 ===\n\n";

dump($xpath, '//c:c1', $c2);
dump($xpath, '//c:c2', $c2);
dump($xpath, '//d:d', $c2);

?>
--EXPECT--
=== Tests with c1 ===

--- //b:bar ---
nodeName: string(5) "b:bar"
prefix: string(1) "b"
namespaceURI: string(5) "urn:b"
--- //c:c1 ---
nodeName: string(4) "c:c1"
prefix: string(1) "c"
namespaceURI: string(6) "urn:c1"
--- //c:c2 ---
No nodes found
--- //c:c3 ---
No nodes found
--- //c:c4 ---
nodeName: string(4) "c:c4"
prefix: string(1) "c"
namespaceURI: string(6) "urn:c1"
--- //d:d ---
nodeName: string(3) "d:d"
prefix: string(1) "d"
namespaceURI: string(5) "urn:d"
--- //a ---
No nodes found

=== Tests with c2 ===

--- //c:c1 ---
nodeName: string(4) "c:c1"
prefix: string(1) "c"
namespaceURI: string(6) "urn:c1"
--- //c:c2 ---
No nodes found
--- //d:d ---
nodeName: string(3) "d:d"
prefix: string(1) "d"
namespaceURI: string(5) "urn:d"