xref: /PHP-7.3/ext/xmlreader/tests/003.phpt (revision 782352c5)
1--TEST--
2XMLReader: libxml2 XML Reader, attributes test
3--SKIPIF--
4<?php if (!extension_loaded("xmlreader")) print "skip"; ?>
5--FILE--
6<?php
7$filename = dirname(__FILE__) . '/_003.xml';
8
9$xmlstring = '<?xml version="1.0" encoding="UTF-8"?>
10<books><book num="1" idx="2">book1</book></books>';
11file_put_contents($filename, $xmlstring);
12
13$reader = new XMLReader();
14if (!$reader->open($filename)) {
15	exit();
16}
17
18// Only go through
19while ($reader->read()) {
20	if ($reader->nodeType != XMLREADER::END_ELEMENT) {
21		if ($reader->nodeType == XMLREADER::ELEMENT && $reader->hasAttributes) {
22			$attr = $reader->moveToFirstAttribute();
23			echo $reader->name . ": ";
24			echo $reader->value . "\n";
25
26			if ($reader->getAttribute($reader->name) == $reader->value) {
27				echo "1st attr (num) failed\n";
28			}
29
30
31			$attr = $reader->moveToNextAttribute();
32			echo $reader->name . ": ";
33			echo $reader->value . "\n";
34
35			if ($reader->getAttribute($reader->name) == $reader->value) {
36				echo "2nd attr (idx) failed\n";
37			}
38
39			// Named attribute
40			$attr = $reader->moveToAttribute('num');
41			echo $reader->name . ": ";
42			echo $reader->value . "\n";
43
44			if ($reader->getAttribute('num') == $reader->value) {
45				echo "attr num failed\n";
46			}
47
48			$attr = $reader->moveToAttribute('idx');
49			echo $reader->name . ": ";
50			echo $reader->value . "\n";
51
52			if ($reader->getAttribute('idx') == $reader->value) {
53				echo "attr idx failed\n";
54			}
55
56			// Numeric positions of attributes
57			$attr = $reader->moveToAttributeNo(0);
58			echo $reader->name . ": ";
59			echo $reader->value . "\n";
60
61			if ($reader->getAttributeNo(0) == $reader->value) {
62				echo "attr 0 failed\n";
63			}
64
65			$attr = $reader->moveToAttributeNo(1);
66			echo $reader->name . ": ";
67			echo $reader->value . "\n";
68
69		}
70	}
71}
72$reader->close();
73unlink($filename);
74?>
75===DONE===
76--EXPECT--
77num: 1
78idx: 2
79num: 1
80idx: 2
81num: 1
82idx: 2
83===DONE===
84