015.phpt 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. --TEST--
  2. XMLReader: libxml2 XML Reader, Move cursor to a named attribute within a namespace
  3. --CREDITS--
  4. Mark Baker mark@lange.demon.co.uk at the PHPNW2017 Conference for PHP Testfest 2017
  5. --EXTENSIONS--
  6. xmlreader
  7. --FILE--
  8. <?php
  9. // Set up test data in a new file
  10. $xmlstring = '<?xml version="1.0" encoding="UTF-8"?>
  11. <books xmlns:ns1="http://www.ns1.namespace.org/" xmlns:ns2="http://www.ns2.namespace.org/"><book ns1:num="1" ns2:idx="2" ns1:idx="3" ns2:isbn="4">book1</book></books>';
  12. $filename = __DIR__ . '/015.xml';
  13. file_put_contents($filename, $xmlstring);
  14. // Load test data into a new XML Reader
  15. $reader = new XMLReader();
  16. if (!$reader->open($filename)) {
  17. exit('XML could not be read');
  18. }
  19. // Parse the data
  20. while ($reader->read()) {
  21. if ($reader->nodeType != XMLREADER::END_ELEMENT) {
  22. // Find the book node
  23. if ($reader->nodeType == XMLREADER::ELEMENT && $reader->name == 'book') {
  24. $attr = $reader->moveToFirstAttribute();
  25. $attr = $reader->moveToAttributeNs('idx', 'http://www.ns1.namespace.org/');
  26. echo $reader->name . ": ";
  27. echo $reader->value . "\n";
  28. $attr = $reader->moveToAttributeNs('idx', 'http://www.ns2.namespace.org/');
  29. echo $reader->name . ": ";
  30. echo $reader->value . "\n";
  31. $attr = $reader->moveToAttributeNs('isbn', 'http://www.ns2.namespace.org/');
  32. echo $reader->name . ": ";
  33. echo $reader->value . "\n";
  34. // Try moving to an attribute that doesn't exist
  35. $attr = $reader->moveToAttributeNs('elephpant', 'http://www.ns2.namespace.org/');
  36. // That move should return a result of false, because there is no elephpant attribute (in any namespace)
  37. if (!$attr) {
  38. echo "Attribute does not exist\n";
  39. }
  40. // Node pointer should still be aat the last valid node
  41. echo $reader->name . ": ";
  42. echo $reader->value . "\n";
  43. }
  44. }
  45. }
  46. // clean up
  47. $reader->close();
  48. ?>
  49. --CLEAN--
  50. <?php
  51. unlink(__DIR__.'/015.xml');
  52. ?>
  53. --EXPECT--
  54. ns1:idx: 3
  55. ns2:idx: 2
  56. ns2:isbn: 4
  57. Attribute does not exist
  58. ns2:isbn: 4