echo "Chapter {$ii} has title '{$title}' with id '{$id}'.\n"; } } /* * To retrieve all DOMNode in one operation there is the ->asArray() method. */ $chapters_nodes = $chapters->asArray(); // Returns an array of DOMNode. echo "————————————————————————————————————————————————————————————————————————————————\n"; /*************** * Namespaces. * ****************/ /* * To use a namespace you have to register its identifier together with its uri. */ $xhtml = fluidns('xhtml', 'http://www.w3.org/1999/xhtml'); $book->namespace($xhtml)->namespace('svg', 'http://www.w3.org/2000/svg')->namespace('xsl', 'http://www.w3.org/TR/xsl', FluidNamespace::MODE_IMPLICIT)->add('xhtml:h1')->add(['xsl:template' => ['xsl:variable']])->query('//xhtml:h1')->add('svg:shape'); echo $book->xml(); echo "————————————————————————————————————————————————————————————————————————————————\n"; /******************* * Removing Nodes. * *******************/ $food->remove('//egg'); // Removes all the eggs. // Which is the same of // $food->query('//egg')->remove(); // Removes all the eggs using a query. // $food->query('/doc')->remove('egg'); // Removes all the eggs using a relative XPath. /* ->remove(...$xpath) * accepts the same arguments of * ->query(...$xpath) * Remember that, like `->query()`, even `->remove()` accepts multiple XPath strings.
assert_is_a($actual, FluidContext::class); $actual = $xml->appendChild('child', 'value', true); assert_is_a($actual, FluidContext::class); $actual = $xml->appendChild(['child1', 'child2'], true); assert_is_a($actual, FluidContext::class); $actual = $xml->appendChild(['child1' => 'value1', 'child2' => 'value2'], true); assert_is_a($actual, FluidContext::class); $actual = $xml->appendChild('child', ['attr' => 'value'], true); assert_is_a($actual, FluidContext::class); $actual = $xml->appendChild(['child1', 'child2'], ['attr' => 'value'], true); assert_is_a($actual, FluidContext::class); }); it('should add namespaced children', function () { $xml = new FluidXml(); $xml->namespace(new FluidNamespace('x', 'x.com')); $xml->namespace(fluidns('xx', 'xx.com', FluidNamespace::MODE_IMPLICIT)); $xml->appendChild('x:xTag1', true)->appendChild('x:xTag2'); $xml->appendChild('xx:xxTag1', true)->appendChild('xx:xxTag2')->appendChild('tag3'); $expected = "<doc>\n" . " <x:xTag1 xmlns:x=\"x.com\">\n" . " <x:xTag2/>\n" . " </x:xTag1>\n" . " <xxTag1 xmlns=\"xx.com\">\n" . " <xxTag2/>\n" . " <tag3/>\n" . " </xxTag1>\n" . "</doc>"; assert_equal_xml($xml, $expected); }); }); describe('.prependSibling', function () { it('should add more than one root node to a document with one root node', function () { $xml = new FluidXml(); $xml->prependSibling('meta'); $xml->prependSibling('extra'); $cx = $xml->query('/*'); $actual = $cx[0]->nodeName; $expected = 'extra'; assert($actual === $expected, __($actual, $expected));