xref: /PHP-7.2/ext/dom/tests/domelement.phpt (revision f1d7e3ca)
1--TEST--
2Elements: DOMElement functionality
3--SKIPIF--
4<?php require_once('skipif.inc'); ?>
5--FILE--
6<?php
7require_once("dom_test.inc");
8
9$dom = new DOMDocument;
10$dom->loadXML($xmlstr);
11if(!$dom) {
12  echo "Error while parsing the document\n";
13  exit;
14}
15
16$node = $dom->documentElement;
17echo "Tag Name: ".$node->tagName."\n";
18
19
20$node->setAttribute('num', '1');
21echo "Chapter: ".$node->getAttribute('num')."\n";
22echo 'Attribute num exists?: '.($node->hasAttribute('num')?'Yes':'No')."\n";
23$node->removeAttribute('num');
24echo "Chapter: ".$node->getAttribute('num')."\n";
25echo 'Attribute num exists?: '.($node->hasAttribute('num')?'Yes':'No')."\n";
26
27echo "Language: ".$node->getAttribute('language')."\n";
28$lang = $node->getAttributeNode('language');
29$lang->nodeValue = 'en-US';
30$node->setAttributeNode($lang);
31echo "Language: ".$node->getAttribute('language')."\n";
32$node->removeAttributeNode($lang);
33echo "Language: ".$node->getAttribute('language')."\n";
34
35echo "\n-- xml:lang --\n";
36$node->setAttributeNS('http://www.w3.org/XML/1998/namespace', 'xml:lang', 'en');
37echo "Language: ".$node->getAttributeNS('http://www.w3.org/XML/1998/namespace', 'lang')."\n";
38echo 'Attribute xml:lang exists?: '.($node->hasAttributeNS('http://www.w3.org/XML/1998/namespace', 'lang')?'Yes':'No')."\n";
39
40$node->removeAttributeNS('http://www.w3.org/XML/1998/namespace', 'lang');
41echo "Language: ".$node->getAttributeNS('http://www.w3.org/XML/1998/namespace', 'lang')."\n";
42echo 'Attribute xml:lang exists?: '.($node->hasAttributeNS('http://www.w3.org/XML/1998/namespace', 'lang')?'Yes':'No')."\n";
43
44$lang = $dom->createAttributeNS('http://www.w3.org/XML/1998/namespace', 'xml:lang');
45$lang->nodeValue = 'en-GB';
46$node->setAttributeNodeNS($lang);
47unset($lang);
48echo "Language: ".$node->getAttributeNS('http://www.w3.org/XML/1998/namespace', 'lang')."\n";
49$lang = $node->getAttributeNodeNS('http://www.w3.org/XML/1998/namespace', 'lang');
50echo "Language: ".$lang->value."\n";
51
52echo "\n-- Elements --\n";
53$rows = $node->getElementsByTagName('row');
54echo "Row Count: ".$rows->length."\n";
55
56$element_ns = new DOMElement('newns:myelement', 'default content', 'urn::dummyns');
57$node->appendChild($element_ns);
58$element_ns = new DOMElement('newns2:myelement', 'second default content', 'urn::dummyns');
59$node->appendChild($element_ns);
60
61$myelements = $node->getElementsByTagNameNS('urn::dummyns', 'myelement');
62$mylen = $myelements->length;
63echo "myelements Count: ".$mylen."\n";
64
65echo "\n-- IDs --\n";
66$node->setAttribute('idatt', 'n1');
67$node->setIdAttribute('idatt', TRUE);
68
69for ($x = 0; $x < $mylen; $x++) {
70	$current = $myelements->item($x);
71	$current->setAttributeNS('urn::dummyns', 'newns:idatt', 'n'.($x+2))."\n";
72	$current->setIdAttributeNS('urn::dummyns', 'idatt', TRUE);
73}
74
75echo 'Element Name: '.(($elem = $dom->getElementByID('n1'))?$elem->localName:'Not Found')."\n";
76$idatt = $node->getAttributeNode('idatt');
77$node->setIdAttributeNode($idatt, FALSE);
78echo 'Element Name: '.(($elem = $dom->getElementByID('n1'))?$elem->localName:'Not Found')."\n";
79
80echo 'Element Name: '.(($elem = $dom->getElementByID('n3'))?$elem->nodeName:'Not Found')."\n";
81for ($x = 0; $x < $mylen; $x++) {
82	$node = $myelements->item($x);
83	$node->setIdAttributeNS('urn::dummyns', 'idatt', FALSE);
84}
85echo 'Element Name: '.(($elem = $dom->getElementByID('n3'))?$elem->nodeName:'Not Found')."\n";
86?>
87--EXPECT--
88Tag Name: chapter
89Chapter: 1
90Attribute num exists?: Yes
91Chapter:
92Attribute num exists?: No
93Language: en
94Language: en-US
95Language:
96
97-- xml:lang --
98Language: en
99Attribute xml:lang exists?: Yes
100Language:
101Attribute xml:lang exists?: No
102Language: en-GB
103Language: en-GB
104
105-- Elements --
106Row Count: 3
107myelements Count: 2
108
109-- IDs --
110Element Name: chapter
111Element Name: Not Found
112Element Name: newns2:myelement
113Element Name: Not Found
114