PHP: XML carriage return objects are automatically created using SimpleXML and xpath

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()'); // Array of elements within 'content'.
    foreach ($topicContents as $contentElement) {
        echo $contentElement->asXML();
    }
}
?>

Excerpt from XHTML code output with automatically generated XML carriage return:

<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;
+3
1

, libxml \r .

<?php
$xml = <<< XML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
    <head>
        <title>...</title>
    </head>
    <body><pre>a\nb\r\nc</pre></body>
</html>
XML;
$content = new DOMDocument(); $content->loadhtml($xml); $content = simplexml_import_dom($content); echo $content->asxml();
prints
<html lang="en"><head><title>...</title></head><body><pre>a
b&#13;
c</pre></body></html>
(\n " ", \r\n # 13;\n)
XML, , http://www.w3.org/TR/REC-xml/#sec-line-ends
, XML- ( ) , #xD #xA, #xD, #xA, xA.
\r\n \n, . , ...
+2

Source: https://habr.com/ru/post/1711413/


All Articles