It can be a problem with a too small value of max_execution_time
. If your sitemap is too large, there is no enough time to generate it fully. Thus the document breaks in the middle.
You can add ini_set('max_execution_time', 10);
in wp-config.php
or add max_execution_time = 1
in php.ini
Also, problem can be with extrace space before <?php
or after ?>
in your functions.php
file