1--TEST-- 2SimpleXML: XPath and attributes 3--SKIPIF-- 4<?php if (!extension_loaded("simplexml")) print "skip"; ?> 5--FILE-- 6<?php 7 8$xml =<<<EOF 9<?xml version='1.0'?> 10<root> 11<elem attr1='11' attr2='12' attr3='13'/> 12<elem attr1='21' attr2='22' attr3='23'/> 13<elem attr1='31' attr2='32' attr3='33'/> 14</root> 15EOF; 16 17$sxe = simplexml_load_string($xml); 18 19function test($xpath) 20{ 21 global $sxe; 22 23 echo "===$xpath===\n"; 24 var_dump($sxe->xpath($xpath)); 25} 26 27test('elem/@attr2'); 28test('//@attr2'); 29test('//@*'); 30test('elem[2]/@attr2'); 31 32?> 33===DONE=== 34--EXPECTF-- 35===elem/@attr2=== 36array(3) { 37 [0]=> 38 object(SimpleXMLElement)#%d (1) { 39 ["@attributes"]=> 40 array(1) { 41 ["attr2"]=> 42 string(2) "12" 43 } 44 } 45 [1]=> 46 object(SimpleXMLElement)#%d (1) { 47 ["@attributes"]=> 48 array(1) { 49 ["attr2"]=> 50 string(2) "22" 51 } 52 } 53 [2]=> 54 object(SimpleXMLElement)#%d (1) { 55 ["@attributes"]=> 56 array(1) { 57 ["attr2"]=> 58 string(2) "32" 59 } 60 } 61} 62===//@attr2=== 63array(3) { 64 [0]=> 65 object(SimpleXMLElement)#%d (1) { 66 ["@attributes"]=> 67 array(1) { 68 ["attr2"]=> 69 string(2) "12" 70 } 71 } 72 [1]=> 73 object(SimpleXMLElement)#%d (1) { 74 ["@attributes"]=> 75 array(1) { 76 ["attr2"]=> 77 string(2) "22" 78 } 79 } 80 [2]=> 81 object(SimpleXMLElement)#%d (1) { 82 ["@attributes"]=> 83 array(1) { 84 ["attr2"]=> 85 string(2) "32" 86 } 87 } 88} 89===//@*=== 90array(9) { 91 [0]=> 92 object(SimpleXMLElement)#%d (1) { 93 ["@attributes"]=> 94 array(1) { 95 ["attr1"]=> 96 string(2) "11" 97 } 98 } 99 [1]=> 100 object(SimpleXMLElement)#%d (1) { 101 ["@attributes"]=> 102 array(1) { 103 ["attr2"]=> 104 string(2) "12" 105 } 106 } 107 [2]=> 108 object(SimpleXMLElement)#%d (1) { 109 ["@attributes"]=> 110 array(1) { 111 ["attr3"]=> 112 string(2) "13" 113 } 114 } 115 [3]=> 116 object(SimpleXMLElement)#%d (1) { 117 ["@attributes"]=> 118 array(1) { 119 ["attr1"]=> 120 string(2) "21" 121 } 122 } 123 [4]=> 124 object(SimpleXMLElement)#%d (1) { 125 ["@attributes"]=> 126 array(1) { 127 ["attr2"]=> 128 string(2) "22" 129 } 130 } 131 [5]=> 132 object(SimpleXMLElement)#%d (1) { 133 ["@attributes"]=> 134 array(1) { 135 ["attr3"]=> 136 string(2) "23" 137 } 138 } 139 [6]=> 140 object(SimpleXMLElement)#%d (1) { 141 ["@attributes"]=> 142 array(1) { 143 ["attr1"]=> 144 string(2) "31" 145 } 146 } 147 [7]=> 148 object(SimpleXMLElement)#%d (1) { 149 ["@attributes"]=> 150 array(1) { 151 ["attr2"]=> 152 string(2) "32" 153 } 154 } 155 [8]=> 156 object(SimpleXMLElement)#%d (1) { 157 ["@attributes"]=> 158 array(1) { 159 ["attr3"]=> 160 string(2) "33" 161 } 162 } 163} 164===elem[2]/@attr2=== 165array(1) { 166 [0]=> 167 object(SimpleXMLElement)#%d (1) { 168 ["@attributes"]=> 169 array(1) { 170 ["attr2"]=> 171 string(2) "22" 172 } 173 } 174} 175===DONE=== 176