Skip to content

Element ApplyNamespaces

Thomas Weinert edited this page Jul 12, 2018 · 4 revisions

FluentDOM\DOM\Element::applyNamespaces()

(FluentDOM 5.1)

FluentDOM\DOM\Element appendElement(
  [ string|array $prefixes = NULL ]
)

Adds the (on the document) registered namespaces to the current element.

You need to call this method before appending other nodes. They will get their own namespace attributes otherwise.

Usage

The examples adds the default namespace used in the summary elements to the feed element.

$document = new FluentDOM\DOM\Document();
$document->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
$document->registerNamespace('#default', 'http://www.w3.org/1999/xhtml');

$feed = $document->appendElement('atom:feed');
$feed->applyNamespaces();
$feed->appendElement('atom:title', 'Example Feed');
$feed->appendElement('atom:link', ['href' => 'http://example.org/']);;

$entry = $feed->appendElement('atom:entry');
$entry->appendElement('atom:title', 'Atom-Powered Robots Run Amok');
$entry->appendElement('atom:id', 'urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a');
$entry->appendElement('atom:updated', '2003-12-13T18:30:02Z');
$entry->appendElement('atom:summary')->appendElement('p', 'Some text.');

$document->formatOutput = TRUE;
echo $document->saveXml();

Output

<?xml version="1.0" encoding="UTF-8"?>
<atom:feed xmlns:atom="http://www.w3.org/2005/Atom" xmlns="http://www.w3.org/1999/xhtml">
  <atom:title>Example Feed</atom:title>
  <atom:link href="http://example.org/"/>
  <atom:entry>
    <atom:title>Atom-Powered Robots Run Amok</atom:title>
    <atom:id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</atom:id>
    <atom:updated>2003-12-13T18:30:02Z</atom:updated>
    <atom:summary>
      <p>Some text.</p>
    </atom:summary>
  </atom:entry>
</atom:feed>
Clone this wiki locally