031.phpt 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. --TEST--
  2. SimpleXML: addChild and addAttribute
  3. --EXTENSIONS--
  4. simplexml
  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>
  14. EOF;
  15. $sxe = simplexml_load_string($xml);
  16. /* Add new attribute in a new namespace */
  17. $sxe->addAttribute('v:att11', 'xxx', 'urn::test-v');
  18. /* Try to add attribute again -> display warning as method is for new Attr only */
  19. $sxe->addAttribute('v:att11', 'xxx', 'urn::test-v');
  20. /* Add new attribute w/o namespace */
  21. $sxe->addAttribute('att2', 'no-ns');
  22. $d = $sxe->attributes();
  23. /* Try to add element to attribute -> display warning and do not add */
  24. $d->addChild('m:test', 'myval', 'urn::test');
  25. /* Test adding elements in various configurations */
  26. $sxe->addChild('m:test1', 'myval', 'urn::test');
  27. /* New namespace test */
  28. $n = $sxe->addChild('m:test2', 'myval', 'urn::testnew');
  29. $sxe->addChild('test3', 'myval', 'urn::testnew');
  30. $sxe->addChild('test4', 'myval');
  31. /* Does not add prefix here although name is valid (but discouraged) - change behavior? */
  32. $sxe->addChild('s:test5', 'myval');
  33. echo $sxe->asXML();
  34. ?>
  35. --EXPECTF--
  36. Warning: SimpleXMLElement::addAttribute(): Attribute already exists in %s031.php on line %d
  37. Warning: SimpleXMLElement::addChild(): Cannot add element to attributes in %s031.php on line %d
  38. <?xml version="1.0"?>
  39. <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">
  40. <child1>test</child1>
  41. <child1>test 2</child1>
  42. <s:child3/>
  43. <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>