Using DomDocument() Correctly

I’m trying to append an html excerpt into a valid xhtml strict using php.
Here is my php code:

<?php
$doc = new DomDocument();
$menu = new DomDocument();

$doc -> load(‘html/page.html’); // line 3
$menu -> load(‘xml/menu.xml’);

$parent = $doc -> getElementById(‘menu’);
$child = $menu -> documentElement;

$child = $doc -> importNode($child,true);
$parent -> appendChild($child); // line 8

echo $doc->saveHTML();
?>

I get the error message on line 8:
Fatal error: Call to a member function appendChild() on a non-object…

Then I change line 3 to this:
$doc ->loadHTMLFile(‘html/page.html’);

Aparantly it solves the problem and displays correctly BUT won’t pass W3C Validator. Looking at the source code by a web browser shows this headers:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html lang=“he” xmlns=“http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=“content-type” content=“text/html; charset=utf-8”>
<title>my page</title>
<link rel=“stylesheet” type=“text/css” href=“styles/index.css”>
<script type=“text/javascript” src=“scripts/index.js”></script>
</head>

META element and LINK element loose their self-closing slash character thus validator fails. Any solution?

I found that the method getElementById returns null because it looks for attribute xml:id instead of id which does not exist in the original xhtml file. If I change all id attributes to xml:id attributes then it works fine but then w3c validator finds atribute xml:id not valid. So I came up with this solution. I don’t know if it is recommended but it perfectly works:

<?php
$doc = new DomDocument();
$plugin = new DomDocument();

$doc -> preserveWhitespace=false;
$doc -> load(‘html/calculator.html’);

$tags = $doc->getElementsByTagName(‘*’);
foreach ($tags as $tag)
$tag->setAttribute(‘xml:id’,$tag->getAttribute(‘id’));

$plugin -> load(‘xml/menu.xml’);
$menu = $doc->importNode($plugin->documentElement,true);
if($doc -> getElementById(‘front’) != null)
$doc -> getElementById(‘front’) -> appendChild($menu);

foreach ($tags as $tag)
$tag->removeAttribute(‘xml:id’);

$doc -> formatOutput;
echo $doc->saveHTML();
?>

There are some things that have helped me in the past.

Make sure that your HTML are valid XHTML files, and even include the xml header

<?xml version="1.0" encoding="UTF-8"?>

If all files load properly, then instead of using

echo $doc->saveHTML();

use

echo $doc->saveXML();

The function saveXML outputs valid XHTML markup instead of HTML.

As to the id problem, make sure the <head> tag contains the xmlns declaration.

<html xmlns="http://www.w3.org/1999/xhtml">

This has solved issues like yours for me in the past.

If it doesn’t solve it, can you past the relevant parts of your html and xml files?