Jump to content

Auto-generated XML carriage return entities appear w/ SimpleXML and xpath


nothing500

Recommended Posts

I'm using SimpleXML and xpath to read elements from an external UTF-8 XHTML document. I then iteratively echo the output of SimpleXML's asXML() function executed upon each element returned from an xpath selector. But the XML carriage return entity is annoyingly inserted after every line of my code. There aren't any extra characters in the XHTML document. What is causing this? It seems to be the way I'm iterating through each array element returned from xpath. I don't get the entities when I'm just outputting one element directly from SimpleXML's 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()'); // Array of elements within 'content'.
foreach ($topicContents as $contentElement) {
	echo $contentElement->asXML();
}
}
?>

 

Excerpt from outputted XHTML code with auto-generated XML carriage returns:

<div class="content">&#13;
<p>Lorem ipsum dolor sit amet</p>&#13;
<h2>Lorem ipsum</h2>&#13;
<p>Lorem ipsum dolor sit amet</p>&#13;
<ul>
	<li>Lorem ipsum</li>&#13;
	<li>Lorem ipsum</li>&#13;
	<li>Lorem ipsum</li>&#13;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.