xref: /PHP-7.2/ext/simplexml/tests/031.phpt (revision 17ccbeec)
1--TEST--
2SimpleXML: addChild and addAttribute
3--SKIPIF--
4<?php if (!extension_loaded("simplexml")) print "skip"; ?>
5--FILE--
6<?php
7$xml =<<<EOF
8<root s:att1="b" att1="a"
9      xmlns:s="urn::test" xmlns:t="urn::test-t">
10   <child1>test</child1>
11   <child1>test 2</child1>
12   <s:child3 />
13</root>
14EOF;
15
16$sxe = simplexml_load_string($xml);
17
18/* Add new attribute in a new namespace */
19$sxe->addAttribute('v:att11', 'xxx', 'urn::test-v');
20
21/* Try to add attribute again -> display warning as method is for new Attr only */
22$sxe->addAttribute('v:att11', 'xxx', 'urn::test-v');
23
24/* Add new attribute w/o namespace */
25$sxe->addAttribute('att2', 'no-ns');
26
27$d = $sxe->attributes();
28/* Try to add element to attribute -> display warning and do not add */
29$d->addChild('m:test', 'myval', 'urn::test');
30
31
32/* Test adding elements in various configurations */
33$sxe->addChild('m:test1', 'myval', 'urn::test');
34
35/* New namespace test */
36$n = $sxe->addChild('m:test2', 'myval', 'urn::testnew');
37
38$sxe->addChild('test3', 'myval', 'urn::testnew');
39$sxe->addChild('test4', 'myval');
40
41/* Does not add prefix here although name is valid (but discouraged) - change behavior? */
42$sxe->addChild('s:test5', 'myval');
43
44echo $sxe->asXML();
45?>
46===DONE===
47--EXPECTF--
48Warning: SimpleXMLElement::addAttribute(): Attribute already exists in %s031.php on line %d
49
50Warning: SimpleXMLElement::addChild(): Cannot add element to attributes in %s031.php on line %d
51<?xml version="1.0"?>
52<root xmlns:s="urn::test" xmlns:t="urn::test-t" xmlns:v="urn::test-v" s:att1="b" att1="a" v:att11="xxx" att2="no-ns">
53   <child1>test</child1>
54   <child1>test 2</child1>
55   <s:child3/>
56<s:test1>myval</s:test1><m:test2 xmlns:m="urn::testnew">myval</m:test2><test3 xmlns="urn::testnew">myval</test3><test4>myval</test4><test5>myval</test5></root>
57===DONE===
58