I have piece of PHP code.
PHP:
$Implementation = new DOMImplementation();
$Document = $Implementation->createDocument( NULL, NULL, $Implementation->createDocumentType( 'html' ) );
$Document->encoding = 'utf-8';
$Document->loadXML( $main_html[ 'main' ] ); // load main.html
$Document->xinclude();
$Fragment = $Document->createDocumentFragment();
$Fragment->appendXML( $main_html[ 'page' ] ); // load page.html
$Document->getElementById( 'content' )->appendChild( $Fragment );
...
Everything works well except last line, appears error:
PHP Fatal error: Call to a member function appendChild() on a non-object
It seems getElementById() method doesn’t work for $Document.
Look at the HTML.
HTML (main.html):
...
</head>
<body xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include href="wrapper.html" />
</body>
</html>
HTML (wrapper.html):
<section
id="wrapper"
xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include href="header.html" />
<xi:include href="nav.html" />
<xi:include href="content.html" />
<xi:include href="aside.html" />
<xi:include href="footer.html" />
</section>
HTML (content.html):
<section id="content" />
I added $Document->validateOnParse = TRUE; before $Document->loadXML( $main_html[ 'main' ] ); and tested without XInclude, but doesn`t work.
Finally I found the solution, bad line replace with:
$Document->getElementsByTagName( 'section' )->item( 1 )->appendChild( $Fragment );
getElementsByTagName() method works for $Document but doesn’t satisfy me. Did I missed something?
You have to pass a DTD which defines the
idattribute. Otherwise, you cannot usegetElementById.For more information, see: