240

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
  • 98,039
  • 18
  • 159
  • 218
Eslam Hamdy
  • 6,706
  • 26
  • 101
  • 162
  • 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
  • 1
    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

15 Answers15

472

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
  • 98,039
  • 18
  • 159
  • 218
  • 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
  • 2
    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
  • It's amazing what a simple blank space can do :/ Thanks! – Edenshaw Dec 02 '19 at 20:26
  • 1
    Just saved me a lot of time to find the reason. – Shtefan Mar 08 '22 at 05:49
54

Reason for me is 2 of following code in one xml

<?xml version="1.0" encoding="utf-8"?>
Anuj Jindal
  • 1,703
  • 15
  • 23
15

Check if you have this line repeated in your XML file

<?xml version="1.0" encoding="utf-8"?>
Kamau Mbûgua
  • 647
  • 8
  • 17
14

check your xml if your xml contain following line double remove one line(the duplicte line)

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

Remove the one duplicate line

<?xml version="1.0" encoding="utf-8"?>
Mazhar Iqbal
  • 565
  • 5
  • 7
  • This point has already been made multiple times over the years, including the [original answer](https://stackoverflow.com/a/19898942/290085) in 2013: ***#3: Resolution:** Search for ` – kjhughes Nov 15 '21 at 13:54
  • ther is a duplicate xml version line in my code by removing its work for me...i dont know why you commnet like that sorry – Mazhar Iqbal Nov 16 '21 at 09:41
  • as far as i was facing this problem i just check by myself in my xml i found this and remove the duplicate line which work for me..thats why i answer but i dont know why you vote like that thank you – Mazhar Iqbal Nov 16 '21 at 09:45
  • You should only answer a question if you have something new or different to contribute. Answering what's already been answered the same way clutters the site and disrespects previous work rather than celebrating it properly with an upvote. – kjhughes Nov 16 '21 at 14:11
  • i dit read any answer i just answer my own experience which work for me – Mazhar Iqbal Nov 17 '21 at 04:54
  • Therein lies the problem. You should read existing answers before answering. If an existing answer covers what you've found, upvote it. Do not post an answer that contributes nothing new. – kjhughes Nov 17 '21 at 04:57
7

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,783
  • 9
  • 45
  • 82
  • 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
  • 38,157
  • 14
  • 182
  • 142
  • 2
    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
2

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.
  • 7,940
  • 3
  • 33
  • 30
1

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
  • 233
  • 1
  • 3
  • 14
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,042
  • 2
  • 20
  • 29
1

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

while(ob_get_level()) ob_end_clean();
Beachhouse
  • 4,826
  • 3
  • 24
  • 36
1

In my case, I removed all blank space without needed code. And it worked

Abir Ahsan
  • 1,776
  • 15
  • 31
1

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

Robin
  • 855
  • 7
  • 22
  • 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
0

This error really break the memory efficiency of the XMLEventReader because fail when there is white spaces at the beginner of the input.

Any way, because is already coded to use it, I have to convert the InputStream or Reader source to String then call trim() to create another reader with this String to create the XML event reader.

Pseudocodes:

Reader reader = new StringReader(
        inputStreamToStringBuilder( getInputStream(), getContentCharset() )
                .toString().trim()
);
XMLEventReader eventReader = inputFactory.createXMLEventReader(reader);
Daniel De León
  • 12,505
  • 5
  • 78
  • 69
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,237
  • 1
  • 14
  • 35
0

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.