I use SimpleXML and xpath to read elements from an external XHTML UTF-8 document. Then I repeat the output of the SimpleXML asXML () function that is executed for each element returned from the xpath selector. But the XML carriage return object is annoyingly pasted after every line of my code . There are no extra characters in the XHTML document. What causes this? It seems that I iterate through each element of the array returned from xpath . I do not get objects when I am just outputting one element directly from SimpleXML asXML () (without using xpath).
<?php
$content = new DOMDocument();
$content->loadHTMLFile(CONTENT.html);
$story = simplexml_import_dom($content->getElementById('story'));
$topics = $story->xpath('div[@class="topic"]');
foreach ($topics as $topic) {
$topicContents = $topic->xpath('div/child::node()');
foreach ($topicContents as $contentElement) {
echo $contentElement->asXML();
}
}
?>
Excerpt from XHTML code output with automatically generated XML carriage return:
<div class="content">
<p>Lorem ipsum dolor sit amet</p>
<h2>Lorem ipsum</h2>
<p>Lorem ipsum dolor sit amet</p>
<ul>
<li>Lorem ipsum</li>
<li>Lorem ipsum</li>
<li>Lorem ipsum</li>