Skip to content

Getting Started: 8. Namespaces

Daniele Orlando edited this page Feb 21, 2016 · 4 revisions

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 a FluidNamespace 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 of FluidNamespace instances,
so that multiple namespaces can be registered in one method call.

Clone this wiki locally