Friday, February 20, 2009

Content is not allowed in prolog.

If you see the "Content is not allowed in prolog" error trying to transform xml and xsl the various problems I have found that have caused it include:

1. The wrong type of encoding - for instance, the opening XML tag specifies utf-16 and the format is actually utf-8 in the first line of the document:

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

2. Text before the root which is supposed to be enclosed in an xml tag. For instance:

<?xml version="1.0" encoding="utf-8"?>
This text is in the wrong place
<root>
<element>text</element>
</root>

3. Something else wrong with the opening xml tag such as a problem with references to dtd, etc.

4. You think you're passing XML string in a variable but you're actually passing in a file name or some other String by mistake. Check the value of your variable and make sure it contains valid XML.

5. Other people have mentioned special characters inserted at the beginning of Microsoft documents that you don't typically see - Byte-Order-Mark or BOM. Use a product to edit text files that does not insert these special characters. I have not had this problem using Notepad on Windows.