xref: /PHP-8.3/ext/simplexml/tests/031.phpt (revision 7f2f0c00)
1--TEST--
2SimpleXML: addChild and addAttribute
3--EXTENSIONS--
4simplexml
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--EXPECTF--
47Warning: SimpleXMLElement::addAttribute(): Attribute already exists in %s031.php on line %d
48
49Warning: SimpleXMLElement::addChild(): Cannot add element to attributes in %s031.php on line %d
50<?xml version="1.0"?>
51<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">
52   <child1>test</child1>
53   <child1>test 2</child1>
54   <s:child3/>
55<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>
56