xref: /PHP-7.4/ext/xmlreader/tests/003.phpt (revision 26dfce7f)
1--TEST--
2XMLReader: libxml2 XML Reader, attributes test
3--SKIPIF--
4<?php if (!extension_loaded("xmlreader")) print "skip"; ?>
5--FILE--
6<?php
7$filename = __DIR__ . '/_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			var_dump($reader->moveToAttributeNo(20));
70			var_dump($reader->moveToAttribute('missing-attribute'));
71			var_dump($reader->moveToAttribute(''));
72		}
73	}
74}
75$reader->close();
76unlink($filename);
77?>
78===DONE===
79--EXPECTF--
80num: 1
81idx: 2
82num: 1
83idx: 2
84num: 1
85idx: 2
86bool(false)
87bool(false)
88
89Warning: XMLReader::moveToAttribute(): Attribute Name is required in %s on line %d
90bool(false)
91===DONE===
92