269

This error,

The processing instruction target matching "[xX][mM][lL]" is not allowed

occurs whenever I run an XSLT page that begins as follows:

<?xml version="1.0" encoding="windows-1256"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:include href="../header.xsl"/>
  <xsl:template match="/">
    <xsl:call-template name="pstyle"/>
    <xsl:call-template name="Validation"/>
    <xsl:variable name="strLang">
      <xsl:value-of select="//lang"/>
    </xsl:variable>
    <!-- ////////////// Page Title ///////////// -->
    <title>
        <xsl:value-of select="//ListStudentFinishedExam.Title"/>
    </title>

Note: I removed any leading spaces before the first line, but the error still occurs!

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Eslam Hamdy
  • 7,126
  • 27
  • 105
  • 165
  • 4
    **This error is not particular to XSLT; it relates to XML parsing in general.** Check not just your main XSLT file, but also any included/imported XSLT files (`../header.xsl`) as well as the XML input document being transformed, *[for each of the **three** possibilities I list in my answer below](http://stackoverflow.com/a/19898942/290085)*. – kjhughes Dec 11 '15 at 12:36
  • 2
    In my experience, there was a new empty line added to the beginning of the file accidentally. After removing that it worked fine. So I agree with the first comment. – Dinidu Hewage May 04 '21 at 01:16

10 Answers10

528

Xerces-based tools will emit the following error

The processing instruction target matching "[xX][mM][lL]" is not allowed.

when an XML declaration is encountered anywhere other than at the top of an XML file.

This is a valid diagnostic message; other XML parsers should issue a similar error message in this situation.

To correct the problem, check the following possibilities:

  1. Some blank space or other visible content exists before the <?xml ?> declaration.

    Resolution: remove blank space or any other visible content before the XML declaration.

  2. Some invisible content exists before the <?xml ?> declaration. Most commonly this is a Byte Order Mark (BOM).

    Resolution: Remove the BOM using techniques such as those suggested by the W3C page on the BOM in HTML.

  3. A stray <?xml ?> declaration exists within the XML content. This can happen when XML files are combined programmatically or via cut-and-paste. There can only be one <?xml ?> declaration in an XML file, and it can only be at the top.

    Resolution: Search for <?xml in a case-insensitive manner, and remove all but the top XML declaration from the file.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • For a stray – user2062207 Nov 11 '14 at 15:27
  • To eliminate a stray XML declaration, you have to treat the file as text, not XML, because an extra XML declaration prevents the XML from being [**well-formed**](http://stackoverflow.com/a/25830482/290085). Use a text editor or open the file programmatically and operate on it as *text* to eliminate the stray **``** declaration *before* treating it as an XML file. – kjhughes Nov 11 '14 at 17:20
  • Open manifest.xml press `ctrl+A` then `ctrl+I` . It will auto format your manifest.xml. – Faisal Naseer Apr 02 '15 at 15:46
  • @FaisalNaseer: Which of the three listed possibilities does your comment address, and from what application are you suggesting that `ctrl+A` and `ctrl+I` be pressed? The resolutions listed in this answer are independent of any particular application. – kjhughes Apr 02 '15 at 16:05
  • @kjhughes sorry for not being specific its related to Eclipse IDE as it causes auto indenting to the statements so if any extra character usually an unconsidered space character causing particular problem will get fixed. – Faisal Naseer Apr 03 '15 at 10:25
  • I am having a peculiar problem with this error. I download a file automatically, save it and process it to store the contents in a database. As you mentioned, my xml file has a xml declaration at the middle of the file. But, the error does not always appear. If it fails first time and I re-run the code, it passes. Sometimes, it fails 2-3 times and passes when I run it for the next time. In production environment, it has never failed, even though I am downloading the same file and using the same code. – Goku__ Jun 11 '15 at 18:09
  • 4
    You have my sympathies as intermittent problems are notoriously difficult to debug. However, I cannot help more from here other than to tell you that this error is **definitely deterministic**: It will ***always*** be an error for an XML declaration to appear more than once or anywhere other than the top of an XML file. Good luck. – kjhughes Jun 11 '15 at 18:29
  • Thanks, in my case it was enough to remove a comment `` that had been put *before* the ` – maggix Apr 08 '16 at 13:00
  • 3
    If your XML message is stored as a String, you can try doing a trim() on the String before passing it to your SAX Parser. For some reason I was getting XML responses that introduced extra white space at the start, which resulted in the above Xerces error when parsed. – Robert Casey Aug 26 '16 at 16:28
  • 1
    Its an old thread, but this might help someone: After manually copying an xml file from browser and copy pasting into local text file and saving as an xml, we got the same error(the file in question was a pom.xml and the error was received while doing a build on gradle). We found out that there was an empty line at the top of the xml file, even before the tag, removed it and it worked ! – JavaTec Jan 19 '18 at 20:34
  • I had the same Problem on Android Studio, I had duplicated `` on my layout – Doilio Matsinhe Jul 22 '19 at 19:41
  • In my case it was an empty line before "" Thanks a lot for saving me – Janaka Priyadarshana Dec 06 '22 at 19:15
20

Debug your XML file. Either there is space or added extra or fewer tags.

For better understanding build the project through the command line. Windows: gradlew build

In my case, AndroidManifest.xml has a blank space at the very first line

<Empty Row> // This Creates the issue 
<?xml version="1.0" encoding="utf-8"?>
Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85
  • Note further that "added extra or fewer tags" will not result in the subject error message unless those are ahead of the XML declaration. Finally, your "Either..." statement is misleading in that it excludes other possible sources of the error (#2 and #3 described thoroughly [above](http://stackoverflow.com/a/19898942/290085)). – kjhughes Mar 09 '21 at 15:29
6

There was auto generated Copyright message in XML and a blank line before <resources> tag, once I removed it my build was successful.

enter image description here

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
  • 3
    Removal of comments or whitespace ***before*** the XML declaration could eliminate this error, but for the document shown in your image, there is no need to remove the comment or the blank line ***after*** the XML declaration. – kjhughes May 31 '18 at 22:23
6

just remove this line: <?xml version="1.0" encoding="utf-8"?> because this kind of error only come because of this line or you might also check the format of your line according the mentioned line in this answer.

  • **Wrong**. ***Do not blindly remove an XML declaration to try to eliminate this error.*** Understand the [three simple possibilities leading to this error](https://stackoverflow.com/a/19898942/290085) and correct the actual issue by following the provided simple and specific resolution instead. – kjhughes Jan 11 '23 at 14:32
1

Another reason of the above error is corrupted jar file. I got the same error but for Junit when running unit tests. Removing jar and downloading it again fixed the issue.

Peter T.
  • 8,757
  • 3
  • 34
  • 32
1

I had a similar issue with 50,000 rdf/xml files in 5,000 directories (the Project Gutenberg catalog file). I solved it with riot (in the jena distribution)

the directory is cache/epub/NN/nn.rdf (where NN is a number)

in the directory above the directory where all the files are, i.e. in cache

riot epub/*/*.rdf --output=turtle > allTurtle.ttl

This produces possibly many warnings but the result is in a format which can be loaded into jena (using the fuseki web interface).

surprisingly simple (at least in this case).

user855443
  • 2,596
  • 3
  • 25
  • 37
0

in my case was a wrong path in a config file: file was not found (path was wrong) and it came out with this exception:

Error configuring from input stream. Initial cause was The processing instruction target matching "[xX][mM][lL]" is not allowed.

pikimota
  • 241
  • 1
  • 4
  • 15
0

For PHP, put this line of code before you start printing your XML:

while(ob_get_level()) ob_end_clean();
Beachhouse
  • 4,972
  • 3
  • 25
  • 39
0

It's worth checking your server's folders to see if there's a stray pom.xml hanging around.

I found that I had the problem everyone else described with a malformed pom.xml, but in a folder that I didn't expect to be on the server. An old build was sticking around unwelcome D:

Dan Rayson
  • 1,315
  • 1
  • 14
  • 37
0

For my case, the tab is the trouble maker. Replace the tab with blank should resolve the issue

buqing
  • 925
  • 8
  • 25
  • 1
    Note that tab is no different than blank/space in this context: Neither are permitted before the XML declaration. – kjhughes Oct 27 '21 at 15:12