xref: /PHP-5.6/ext/dom/tests/domelement.phpt (revision e94670d4)
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--
88
89Tag Name: chapter
90Chapter: 1
91Attribute num exists?: Yes
92Chapter:
93Attribute num exists?: No
94Language: en
95Language: en-US
96Language:
97
98-- xml:lang --
99Language: en
100Attribute xml:lang exists?: Yes
101Language:
102Attribute xml:lang exists?: No
103Language: en-GB
104Language: en-GB
105
106-- Elements --
107Row Count: 3
108myelements Count: 2
109
110-- IDs --
111Element Name: chapter
112Element Name: Not Found
113Element Name: newns2:myelement
114Element Name: Not Found
115