1--TEST-- 2SimpleXML: XPath and attributes 3--EXTENSIONS-- 4simplexml 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--EXPECTF-- 34===elem/@attr2=== 35array(3) { 36 [0]=> 37 object(SimpleXMLElement)#%d (1) { 38 ["@attributes"]=> 39 array(1) { 40 ["attr2"]=> 41 string(2) "12" 42 } 43 } 44 [1]=> 45 object(SimpleXMLElement)#%d (1) { 46 ["@attributes"]=> 47 array(1) { 48 ["attr2"]=> 49 string(2) "22" 50 } 51 } 52 [2]=> 53 object(SimpleXMLElement)#%d (1) { 54 ["@attributes"]=> 55 array(1) { 56 ["attr2"]=> 57 string(2) "32" 58 } 59 } 60} 61===//@attr2=== 62array(3) { 63 [0]=> 64 object(SimpleXMLElement)#%d (1) { 65 ["@attributes"]=> 66 array(1) { 67 ["attr2"]=> 68 string(2) "12" 69 } 70 } 71 [1]=> 72 object(SimpleXMLElement)#%d (1) { 73 ["@attributes"]=> 74 array(1) { 75 ["attr2"]=> 76 string(2) "22" 77 } 78 } 79 [2]=> 80 object(SimpleXMLElement)#%d (1) { 81 ["@attributes"]=> 82 array(1) { 83 ["attr2"]=> 84 string(2) "32" 85 } 86 } 87} 88===//@*=== 89array(9) { 90 [0]=> 91 object(SimpleXMLElement)#%d (1) { 92 ["@attributes"]=> 93 array(1) { 94 ["attr1"]=> 95 string(2) "11" 96 } 97 } 98 [1]=> 99 object(SimpleXMLElement)#%d (1) { 100 ["@attributes"]=> 101 array(1) { 102 ["attr2"]=> 103 string(2) "12" 104 } 105 } 106 [2]=> 107 object(SimpleXMLElement)#%d (1) { 108 ["@attributes"]=> 109 array(1) { 110 ["attr3"]=> 111 string(2) "13" 112 } 113 } 114 [3]=> 115 object(SimpleXMLElement)#%d (1) { 116 ["@attributes"]=> 117 array(1) { 118 ["attr1"]=> 119 string(2) "21" 120 } 121 } 122 [4]=> 123 object(SimpleXMLElement)#%d (1) { 124 ["@attributes"]=> 125 array(1) { 126 ["attr2"]=> 127 string(2) "22" 128 } 129 } 130 [5]=> 131 object(SimpleXMLElement)#%d (1) { 132 ["@attributes"]=> 133 array(1) { 134 ["attr3"]=> 135 string(2) "23" 136 } 137 } 138 [6]=> 139 object(SimpleXMLElement)#%d (1) { 140 ["@attributes"]=> 141 array(1) { 142 ["attr1"]=> 143 string(2) "31" 144 } 145 } 146 [7]=> 147 object(SimpleXMLElement)#%d (1) { 148 ["@attributes"]=> 149 array(1) { 150 ["attr2"]=> 151 string(2) "32" 152 } 153 } 154 [8]=> 155 object(SimpleXMLElement)#%d (1) { 156 ["@attributes"]=> 157 array(1) { 158 ["attr3"]=> 159 string(2) "33" 160 } 161 } 162} 163===elem[2]/@attr2=== 164array(1) { 165 [0]=> 166 object(SimpleXMLElement)#%d (1) { 167 ["@attributes"]=> 168 array(1) { 169 ["attr2"]=> 170 string(2) "22" 171 } 172 } 173} 174