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