-
Notifications
You must be signed in to change notification settings - Fork 43
Getting Started: 8. Namespaces
XML namespaces are an important part of documents manipulation and FluidXML makes so easy to use them that you will not believe.
Start registering the namespace identifier together with the namespace uri.
$book->namespace('xhtml', 'http://www.w3.org/1999/xhtml')
->namespace('svg', 'http://www.w3.org/2000/svg')
->namespace('xsl', 'http://www.w3.org/TR/xsl', FluidNamespace::MODE_IMPLICIT);
At this point you are ready to use it.
Concise syntax
$book->add('xhtml:h1') ->add([ 'xsl:template' => [ 'xsl:variable' ] ]) ->query('//xhtml:h1') ->add('svg:shape');Extended syntax
$book->addChild('xhtml:h1') ->addChild([ 'xsl:template' => [ 'xsl:variable' ] ]) ->query('//xhtml:h1') ->addChild('svg:shape');
echo $book;
<?xml version="1.0" encoding="UTF-8"?>
<book>
...
<xhtml:h1 xmlns:xhtml="http://www.w3.org/1999/xhtml">
<svg:shape xmlns:svg="http://www.w3.org/2000/svg"/>
</xhtml:h1>
<template xmlns="http://www.w3.org/TR/xsl">
<variable/>
</template>
</book>
That's it! Even XML namespaces can be easy and fun to use.
Pro Tip:
A namespace can be defined even as aFluidNamespace
instance,
to make easy to share namespaces between different documents.Concise syntax
$xhtml = fluidns('xhtml', 'http://www.w3.org/1999/xhtml'); $svg = fluidns('svg', 'http://www.w3.org/2000/svg', FluidNamespace::MODE_IMPLICIT); $book->namespace($xhtml, $svg);Extended syntax
$xhtml = new FluidNamespace('xhtml', 'http://www.w3.org/1999/xhtml'); $svg = new FluidNamespace('svg', 'http://www.w3.org/2000/svg', FluidNamespace::MODE_IMPLICIT); $book->namespace($xhtml, $svg);
namespace()
accepts a variable number ofFluidNamespace
instances,
so that multiple namespaces can be registered in one method call.
An advanced approach consists in registering the namespaces on a specific node, for example the root of the document. It can be achieved using the internal DOM APIs.
Example showing how to set multiple namespaces on the root of the document.
$googNs = fluidns('g', 'http://base.google.com/ns/1.0');
$xslNs = fluidns('xsl', 'http://www.w3.org/1999/XSL/Transform');
$rss = fluidxml(null); // A document without a root node.
$rss->namespace($googNs, $xslNs); // Let's register the namespaces associated with our document.
$root = $rss->addChild('rss', true); // Our document is with no root node, let's create one.
$rootNode = $root[0]; // Accessing the node as array returns the associated DOMNode.
// In this case $root[0] is the first and only associated DOMNode.
// Now we can access the DOMNode APIs.
// Let's add the namespaces on the root node using the DOMNode setAttributeNS() interface.
$rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', "xmlns:{$googNs->id()}", $googNs->uri());
$rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', "xmlns:{$xslNs->id()}", $xslNs->uri());
// Done, the root node is filled with the namespaces declarations.
// We can continue as usual.
$rss->add('channel', true)
->add('title', '...')
->add('link', '...')
->add('g:image', '...')
->add('g:price', '...');