024.phpt 2.7 KB

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