PHP5 DOM, XPath and namespaces

PHP5 DOM

ok, so i've got a little problem.

I've got a template document that is XHTML mixed with special custom tags I've made - not unlike the ASP.NET
style. I use the XML loading functions of PHP5 DOM to get strict XHTML in the end....

Template before:
================

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wc="http://www.justinjmoses.com.au/WC" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MySchool Online</title>
</head>
<body>

<wc:custom>
<form>
</form>
</wc:custom>
</body>
</html>



Now when I load it, fine. But when I create a new element, say, "div" to replace the wc:custom
object, adding the children causes the addition of the namespace blocks. See below:

PHP
===
<php>

$new_node = $document->createElement("div");


$children = $this->templateNode->childNodes;


//append all children to this element
for ($i=0; ($child = $children->item($i)) != null; $i++)
{
$x = $child->cloneNode(true);
$new_node->appendChild($x);
}
</php>

Template after:
===============

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wc="http://www.justinjmoses.com.au/WC" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>MySchool Online</title>
</head>
<body>

<div>

<form xmlns:wc="http://www.justinjmoses.com.au/WC" xmlns:default="http://www.w3.org/1999/xhtml">
</form>
</div>
</body>
</html>


Weird? I noticed some bug notes about this very problem with PHP4 DOMXML, so maybe it isn't a bug. but something
is wrong with my understanding of namespaces???
The reason this is a probelm is because when i try to do an XPath query from within the DIV, it won't
yield the form tag unless i precursor it with a "default" prefix, and set the default prefix via
XPath->registerNamespacePrefix()

Any help would be appreciated.


Thanx,
justin
 
Back
Top