$dom->loadXML($fluid->xml()); $dom->formatOutput = true; $dom->preserveWhiteSpace = false; // DOMDocument import. $fluid = fluidxml($dom); echo $fluid->xml(); echo "————————————————————————————————————————————————————————————————————————————————\n"; $simplexml = new SimpleXMLElement($fluid->xml()); // SimpleXMLElement import. $fluid = fluidxml($simplexml); echo $fluid->xml(); echo "————————————————————————————————————————————————————————————————————————————————\n"; // XML file import. // $fluid = fluidify('path/to/file.xml'); $other = fluidxml($fluid->xml()); $fluid = fluidxml(); $fluid->add($other)->add(['query' => $fluid->query('//meta'), 'dom' => $dom, 'domnodes' => $dom->childNodes, 'simplexml' => $simplexml]); // Imports a SimpleXMLElement. /****************** * XPath Queries. * *******************/ /* * XPath queries can be absolute or relative to the context over they are executed. */ $eggs = $food->query('//egg'); $fruits = $food->query('//fruit[@price="expensive"]'); echo "We have {$eggs->length()} eggs and {$fruits->length()} expensive fruit.\n"; echo "————————————————————————————————————————————————————————————————————————————————\n"; $book->query('//chapter')->attr('lang', 'en')->query('..')->attr('lang', 'en')->query('../title')->attr('lang', 'en'); /* * The previous code presents a repetition: all 'setAttribute' calls are identical.
use function FluidXml\fluidify; use function FluidXml\is_an_xml_string; use function FluidXml\domdocument_to_string_without_headers; use function FluidXml\domnodelist_to_string; use function FluidXml\domnodes_to_string; use function FluidXml\simplexml_to_string_without_headers; describe('fluidxml', function () { it('should behave like FluidXml::__construct', function () { $xml = new FluidXml(); $alias = fluidxml(); $actual = $alias->xml(); $expected = $xml->xml(); \assert($actual === $expected, __($actual, $expected)); $options = ['root' => 'root', 'version' => '1.2', 'encoding' => 'UTF-16', 'stylesheet' => 'stylesheet.xsl']; $xml = new FluidXml($options); $alias = fluidxml($options); $actual = $alias->xml(); $expected = $xml->xml(); \assert($actual === $expected, __($actual, $expected)); }); }); describe('fluidify', function () { it('should behave like FluidXml::load', function () { $doc = '<tag>content</tag>'; $xml = FluidXml::load($doc); $alias = fluidify($doc); $actual = $alias->xml(); $expected = $xml->xml(); \assert($actual === $expected, __($actual, $expected)); }); });
require \dirname(__DIR__) . '/source/FluidXml.php'; $actual = \defined('FLUIDXML_VERSION'); $expected = true; \assert($actual === $expected, __($actual, $expected)); }); }); describe('fluidxml()', function () { it('should behave like FluidXml::__construct()', function () { $xml = new FluidXml(); $alias = fluidxml(); $actual = $alias->xml(); $expected = $xml->xml(); \assert($actual === $expected, __($actual, $expected)); $options = ['root' => 'root', 'version' => '1.2', 'encoding' => 'UTF-16', 'stylesheet' => 'stylesheet.xsl']; $xml = new FluidXml(null, $options); $alias = fluidxml(null, $options); $actual = $alias->xml(); $expected = $xml->xml(); \assert($actual === $expected, __($actual, $expected)); }); }); describe('fluidify()', function () { it('should behave like FluidXml::load()', function () { $ds = \DIRECTORY_SEPARATOR; $file = __DIR__ . "{$ds}..{$ds}sandbox{$ds}.test_fluidify.xml"; $doc = "<root>\n" . " <parent>content</parent>\n" . "</root>"; \file_put_contents($file, $doc); $xml = FluidXml::load($file); $alias = fluidify($file); \unlink($file); $actual = $alias->xml();