XML is including 2 spaces at the start of the file

Example URL :

The XML output is including 2 spaces at the start of the file :


  <?xml version="1.0" encoding="utf-8"?>
--^

The XML is still present, error is thrown when you attempt to view it via a browser or other XML viewer. The issue is also intermittent.

Steps to reproduce :

  1. Visit your endpoint URL and generally the error will occur
  2. If not add the .xml

To get rid of the issue :

  1. Change .xml to .json
  2. Change .json to .xml (or leave blank)

Remove additional spaces where they are located

Essentially some spaces have found there way in where they shouldn't be. I've had a look through but can't seem to locate them.

Find and remove spaces

Locating the additional spaces and removing them.

Comments

marcingy’s picture

Status: Active » Postponed (maintainer needs more info)

Please provide clear steps to reproduce or provide a simple test that clearly shows the bug given this statement - The issue is also intermittent

mikeefreedom’s picture

Hi Marcingy,

I can't post a link to our example as it's a staging site I'm unable to share publicly. However I could email a link and access to you.

As for reproducing the intermittent issue... Another dev has tested the URL 10 times and couldn't reproduce it. Then every 4th or 5th time it would occur.

On my machine it would happen more often than not - both using the same URL served from the same server.

My other dev has also noticed that occasionally opening XML tags are missing the first < e.g.

<uuid>2ad272f3-675e-2c34-ed3a-6570ac92db02</uuid>

Would sometimes be :

uuid>2ad272f3-675e-2c34-ed3a-6570ac92db02</uuid>

Again, steps to successfully reproduce this were pretty much hit refresh until it occurs. Then upon the next refresh is was back to normal.

Let me know how I can send this URL and access details to you and you will be able to see a live example. I'm happy to post my custom module code as well.

mikeefreedom’s picture

We thought the UUID module might not be playing nicely but dropping that out had no effect - gutted.

marcingy’s picture

Status: Postponed (maintainer needs more info) » Active

The best bet sounds like if you can supply the custom code to start with as that might give us some ideas, well there goes my initial suggestion of try disabling the uuid module :(

Also if you could post a copy of your endpoint export that would be great feel free to rename anything that might compromise your site as this will help us in debuging.

mikeefreedom’s picture

Have you got a BitBucket account? We can provide you access to the repo.

It has both the custom code and the exported endpoint on it. We're happy for you to look at it, just not the rest of the public :)

kylebrowning’s picture

Status: Active » Closed (cannot reproduce)

This isn't something service sis doing, this is something a module on your system is doing.

run this ruby script and find the culprit

#!/usr/bin/ruby
 
Dir.glob( '**/*.php' ) do |file|
  puts file if IO.read(file).match( /\?>\s{2,}\Z/m)
end
mikeefreedom’s picture

Sorry for delay.

We've run this test and come back with 3 template files that are unrelated. We're running tests as we speak.

Will this Ruby RegExp check for spaces at the start and end of a file? My RegExp is a bit rusty.

And thank you for providing it :)

Mo Omar’s picture

Hi,
I am sorry but I don't understand anything in this page. I just want to know if my sitemapxml is working or not. Here is a link to my sitemap

When I click on that link, it gives me different pages on different browsers.
On FF, I get this message: "
XML Parsing Error: XML or text declaration not at start of entity
Location: https://twelvestepjournaling.com/sitemap.xsl
Line Number 2, Column 1:<?xml version="1.0" encoding="UTF-8"?>
^
"

When I click edit sitemap from admin side, i get this message: There are currently no XML sitemap contexts available.

Would any of you experts tell me if my sitemap is working or not? Thank you very much!!

ch@it@li’s picture

Hello,

I have same issue.
Please provide me solution.

Thanks

marcingy’s picture

@ch@it@li the issue is nothing to do with services the issue lies somewhere in your code base outside services

jamatulli’s picture

Issue summary: View changes

This issue is a bit too common and does usually show up with XML docs. Someone probably left a space at the beginning of a module.

[space]<?php

You can either try to find that bit of code with some tool or command OR put this in includes/bootstrap.inc at line 1112:

echo $filename;

The line number could change subject to updates to Drupal 7 so make sure it is inside the drupal_load function:

if ($filename) {
    echo $filename;  //this is your included line
    include_once DRUPAL_ROOT . '/' . $filename;
    $files[$type][$name] = TRUE;

Look at the source code for the page (right click on the page and "view source") and you will see a long list of the modules being loaded all run together. The space will be right after the file with the problem. Find that file, open it up and delete the space at the beginning.

yareckon’s picture

jamatulli, thank you! That's a great trick! Your comment is worth its weight in gold.

sarathkm’s picture

Here is another way of using grep to find the culprit. Mostly it is module file so...
grep -R -H '^ <?php' modules/**/*.module
This will check all module files withing your module folder ( i am a step back in my sites/all folder in my linux terminal to find culprit.)
If you find two spaces leave two space between ^ and <?php, if one space leave one.
you will get the path of file which is culprit.

If you just get the filename you can find its path via
locate -br 'culprit.module'

After correcting clear cache. :)

Regards To All

jamatulli’s picture

Very nice. Grep is required but that is fine for all the *nix users.

Could do this as well to catch any number of spaces.

grep -R -H '^[[:space:]]*<?php' *.module

sarathkm’s picture

That is more refined form for finding space. I guess grep is available in Mac OS too.
While for windows we can use findstr in place of grep to find the culprit.

I guess it will go something like this

findstr "^ *<?php" *.module

There is a space between ^ and * so as to match any number of spaces at start of line. Will search all .module files in current and subdirectories with matching pattern.

Or

findstr /b " *<?php" *.module

It will match the pattern at the beginning of line with one or more spaces

findstr Reference

Could try for grep like this too

grep -R -H '^ *<?php' modules/**/*.module