Hi,
I am getting an error when viewing the sitemap.xml:
error on line 2 at column 6: XML declaration allowed only at the start of the document
Looking at the issue queue I see that others have had the exact same error/issue but I never saw any resolution.
Looking at the code it does appear that there is a blank line at the top of the error page (see screenshot).
I get the exact same issue using the XML Sitemap module so I know it's not the module's fault, I'm just hoping to find some insight into the cause of the error/issue as I've spent a couple of days already trying to troubleshoot this.
Per some other threads, I've looked at custom modules and themes to see if there were any spaces before the opening <?php ... there were not.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | output.jpg | 125.68 KB | 3cwebdev |
Comments
Comment #2
3cwebdev commentedComment #3
gbyteI am not sure what the reason is, please check other browsers and also your log. Maybe some error is thrown and something is written to the browser? Also please test with disabled javascript.
Comment #4
gbyteClosing due to lack of activity.
Comment #5
r3m commentedYou have to check if you don't have empty file like "modules/custom/your_module/your_module.module".
All your php files should begin with "<?php" (without space). If you have PHP files completely empty and without <?php, you will have error with all xml pages.
Comment #6
liampower commentedThis is still an issue and the replies above are not an issue. There are no errors being logged either.
I also can't replicate this on any environment or local, other than the production (Hosted with Acquia).
Comment #7
gbyteI've seen this error on web pages outputting XML content and it's always been a problem with another module or the environment. See comment #5. Google spits out lots of results indicating a module/template/script might add a blank line at the beginning of the output.
Try disabling all contributed modules first and run /rebuild.php afterwards. Make sure to clear other layers of caching if you use caching outside of Drupal. If this does not work, ask Acquia for support. It is impossible to debug this for me.
Comment #8
liampower commentedI can confirm that this was a server-specific file that had been edited.
Comment #9
gbyteWhat kind of file was that?
Comment #10
vrwired commentedI went forth to examine all my custom modules and lo and behold, two whitespaces were accidentally been committed before the <?php tag! ... (I need to start using codesniffer on all my projects as I'm sure that would have been detected that mistake on commit.)
Regenerating the xml allowed me to inspect a bunch of new errors on sitemap.xml -- all of them due to webprofiler. No issue though on stage/prod which is where it matters to run anyway.
Thank you for this excellent module - a solid improvement to our site compared to previous usage of xmlsitemap - at last I look forward to implementing my own custom sitemap types :)
Comment #11
gbyteThanks for documenting your findings.
Thanks, your appreciation almost makes the whole effort seem worthwhile.
Comment #13
mahmoud barhouma commentedThanks @R3m It worked with me when I removed all the space in the custom module
Comment #14
kenrbnsn commentedI am reopening this issue.
I've read through the previous comments and have found NO bad php files on my site, yet the sitemap is still being generated with a blank line at the top.
Wouldn't it be simple not to write out a blank line when generating the sitemap?
Drupal: 8.9.16
simple_sitemap: 3.10.0
Comment #15
thhafner commentedHaving the same issue as kenrbnsn. Including the same version numbers.
Comment #16
gbyteThis issue is inherent to how XML is output via PHP and interpreted by your browser and it is not specific to the module. Googling it yields two causes: whitespace before the opening PHP tag or witespace after the closing PHP tag. To fix it, make sure all files and all included files (attached via the include/require statement or via the use statement) do not have whitespace before the opening PHP tag and do not have the closing PHP tag at all, as it is obsolete.
I am open to commiting code that can circumvent this problem (if at all possible), as it is difficult to debug - let me know if you can think of something.
Comment #17
aiglesiasn commentedI had the same issue, and this help me out:
find . -name "*.php" -type f -size +0 -exec gawk 'STARTFILE{if ($0 == "") print FILENAME}' {} +find . -name "*.php" -type f -size +0 -exec gawk 'FNR==1{if ($0 == "") print FILENAME}' {} +In my case I found that the custom web/sites/sites.php file was the faulty file that started with one empty line.
Hope this helps someone.
Source: https://403page.com/solved-broken-sitemap-xml-error-line-2-at-column-6/
Comment #18
david_neil_smith commentedI seem to be suffering the same issue in that the output XML file contains very little as shown below:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/sitemap.xsl"?>
1.0https://mysite.co.uk/
2021-09-01T15:46:01+00:00
daily
I have far less experience than many of the people on this site as I'm a 'joiner' of things rather than a developer and this is a very simple new charity website that I'm trying to get a sitemap generated for - to give Google Search Console.
I have Drupal 9.2.x - whatever the latest version is at present - and have tried both the XMLSITEMAP module and this one too. This states that it has generated a file - i.e. the above - but providing this to Google doesn't work with Google reporting it cannot fetch it. I also thought a sitemap should generate an XML description - for want of a better word - for all your pages, content (around 6 in my case) - but as you can see from the above it's doesn't seem to show any pages.
If anyone please can point me in the right direction to solve this I would be very grateful.
Many thanks
David
Comment #19
gbyte@aiglesiasn Thanks for documenting this! Mind adding some info and the fix to the module's online documentation?
@david_neil_smith You don't seem to have a problem with the module, just a misconfiguration.
The module needs to be configured first so it knows what kind of pages you want it to index; please refer to the readme or the online documentation.
If you can visit the site from a browser private window and see the sitemap, so should google. No idea what the problem could be here, maybe server misconfiguration? In any case, please search through the issues in the queue in case this has been documented already. If it hasn't, please open up a new support request, as this here ticket is about a different problem
Comment #20
thhafner commentedThis was recently solved for us by removing an extra line at the bottom of the php prepend file
/etc/php/7.3/prepend.phpComment #21
mfby2k commentedi have the same issue but solved by removing the empty line at the start of /sites/default/setting.php.
Comment #22
hunchang commentedsame issue and solved with #21 comment by removing the empty line at the start of settings.php
Comment #23
couloir007 commentedThis is still an issue. Does it seem unreasonable that an extraneous space at the beginning or end of a settings.php file should break a module?
Comment #24
sebastian45 commentedI agree with statement #16.
Upon thorough examination of the code, specifically in the .module files, I identified four instances of a potential issue related to blanck space.
After resolving this issue, my sitemap now generates flawlessly.
Comment #25
epicure.club commentedIf you look at epicure.club/sitemap.xml we also get
We do not have an empty space at the beginning or the end of settings.php file. Is this a configuration problem?
Comment #26
pratishjha commented@epicure.club found any solution to fix this? I am also getting same error message