“Content is not allowed in prolog”错误,但在XML声明之前没有任何内容。

7

首先,我已经详尽地检查了以下问题,这个问题似乎不是同样的事情:

所有这些问题似乎都可以归结为两点:

  1. 在开头的 <?xml?> 标记之前存在一个或多个(可能是不可见的)字符。
  2. 在正文中有一些字节序列,它们不符合 <?xml?> 标记中定义的 encoding

至于第一点,我使用 xxd 检查了我的文件,结果显示在这里:

$ xxd sample.fo
00000000: 3c3f 786d 6c20 7665 7273 696f 6e3d 2231  <?xml version="1
00000010: 2e30 2220 656e 636f 6469 6e67 3d22 5554  .0" encoding="UT
00000020: 462d 3822 3f3e 5465 7374 206d 6174 6572  F-8"?>Test mater
00000030: 6961 6c20 6963 6f6e 7354 6869 7320 746f  ial iconsThis to
00000040: 7069 6320 7465 7374 7320 7468 6520 4d61  pic tests the Ma
00000050: 7465 7269 616c 2049 636f 6e73 2e52 4544  terial Icons.RED
00000060: 434f 4d20 4c61 626f 7261 746f 7269 6573  COM Laboratories
00000070: 2c20 496e 632e 0a20 2020 2020 2020 2020  , Inc..
00000080: 2020 2020 2020 2020 2020 2054 6865 7365             These
00000090: 2061 7265 2074 6865 2074 6573 7473 2066   are the tests f
000000a0: 6f72 2074 6865 204d 4920 4449 5441 3a0a  or the MI DITA:.
000000b0: 2020 2020 2020 2020 2020 2020 2020 2020
000000c0: 2020 2020 2020 2020 5465 7374 2074 6865          Test the
000000d0: 2022 6b65 7962 6f61 7264 5f61 7272 6f77   "keyboard_arrow
000000e0: 5f64 6f77 6e22 2069 636f 6e2e 5465 7374  _down" icon.Test
000000f0: 2074 6865 206d 6972 726f 722d 696d 6167   the mirror-imag
00000100: 6520 2272 6570 6c79 2220 6963 6f6e 2e54  e "reply" icon.T
00000110: 6865 2069 636f 6e73 2061 7265 2072 656e  he icons are ren
00000120: 6465 7265 6420 696e 2074 6865 204d 6174  dered in the Mat
00000130: 6572 6961 6c49 636f 6e73 2066 6f6e 742e  erialIcons font.
00000140: 0a09 0954 6573 7420 2331 3a43 6c69 636b  ...Test #1:Click
00000150: 2074 6865 203c 666f 3a69 6e6c 696e 6520   the <fo:inline
00000160: 786d 6c6e 733a 666f 3d22 6874 7470 3a2f  xmlns:fo="http:/
00000170: 2f77 7777 2e77 332e 6f72 672f 3139 3939  /www.w3.org/1999
00000180: 2f58 534c 2f46 6f72 6d61 7422 2066 6f6e  /XSL/Format" fon
00000190: 742d 7765 6967 6874 3d22 626f 6c64 2220  t-weight="bold"
000001a0: 6c69 6e65 2d68 6569 6768 743d 2231 3030  line-height="100
000001b0: 2522 3e3c 666f 3a69 6e6c 696e 6520 786d  %"><fo:inline xm
000001c0: 6c6e 733a 6178 663d 2268 7474 703a 2f2f  lns:axf="http://
000001d0: 7777 772e 616e 7465 6e6e 6168 6f75 7365  www.antennahouse
000001e0: 2e63 6f6d 2f6e 616d 6573 2f58 534c 2f45  .com/names/XSL/E
000001f0: 7874 656e 7369 6f6e 7322 2066 6f6e 742d  xtensions" font-
00000200: 6661 6d69 6c79 3d22 4d61 7465 7269 616c  family="Material
00000210: 4963 6f6e 7322 3eee 8c93 3c2f 666f 3a69  Icons">...</fo:i
00000220: 6e6c 696e 653e 3c2f 666f 3a69 6e6c 696e  nline></fo:inlin
00000230: 653e 2069 636f 6e2e 436c 6963 6b20 7468  e> icon.Click th
00000240: 6520 3c66 6f3a 696e 6c69 6e65 2078 6d6c  e <fo:inline xml
00000250: 6e73 3a66 6f3d 2268 7474 703a 2f2f 7777  ns:fo="http://ww
00000260: 772e 7733 2e6f 7267 2f31 3939 392f 5853  w.w3.org/1999/XS
00000270: 4c2f 466f 726d 6174 2220 666f 6e74 2d77  L/Format" font-w
00000280: 6569 6768 743d 2262 6f6c 6422 206c 696e  eight="bold" lin
00000290: 652d 6865 6967 6874 3d22 3130 3025 223e  e-height="100%">
000002a0: 3c66 6f3a 696e 6c69 6e65 2078 6d6c 6e73  <fo:inline xmlns
000002b0: 3a61 7866 3d22 6874 7470 3a2f 2f77 7777  :axf="http://www
000002c0: 2e61 6e74 656e 6e61 686f 7573 652e 636f  .antennahouse.co
000002d0: 6d2f 6e61 6d65 732f 5853 4c2f 4578 7465  m/names/XSL/Exte
000002e0: 6e73 696f 6e73 2220 666f 6e74 2d66 616d  nsions" font-fam
000002f0: 696c 793d 224d 6174 6572 6961 6c49 636f  ily="MaterialIco
00000300: 6e73 2220 6178 663a 7472 616e 7366 6f72  ns" axf:transfor
00000310: 6d3d 2273 6361 6c65 5828 2d31 2922 3eee  m="scaleX(-1)">.
00000320: 859e 3c2f 666f 3a69 6e6c 696e 653e 3c2f  ..</fo:inline></
00000330: 666f 3a69 6e6c 696e 653e 2069 636f 6e2e  fo:inline> icon.

关于第二个问题,我和file进行了检查:
$ file sample.fo
sample.fo: XML 1.0 document, UTF-8 Unicode text, with very long lines

我只想到两种材质图标字体代码点,它们是3字节的UTF-8字符,并且似乎已经被正确编码了。这两个字符分别是“keyboard_arrow_down”图标,代码点为e313,编码为ee 8c 93,“回复”图标,代码点为e15e,编码为ee 85 9e,在此网站上得到了验证。

根据xxd输出,我的XML标头似乎有效:

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

我还尝试了手动在编码后插入一个空格,这是其他问题答案中建议的方法之一:
<?xml version="1.0" encoding="UTF-8" ?>

这并没有产生任何影响。所以我对问题感到困惑,特别是考虑到给出的错误代码:

[Fatal Error] sample.fo:1:39: Content is not allowed in prolog.
Jul 24, 2018 9:56:34 AM org.apache.fop.cli.Main startFOP
SEVERE: Exception
org.apache.fop.apps.FOPException: org.xml.sax.SAXParseException; systemId: file:/tmp/sample.fo; lineNumber: 1; columnNumber: 39; Content is not allowed in prolog.
javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; systemId: file:/tmp/sample.fo; lineNumber: 1; columnNumber: 39; Content is not allowed in prolog.
        at org.apache.fop.cli.InputHandler.transformTo(InputHandler.java:296)
        at org.apache.fop.cli.InputHandler.renderTo(InputHandler.java:116)
        at org.apache.fop.cli.Main.startFOP(Main.java:186)
        at org.apache.fop.cli.Main.main(Main.java:217)
Caused by: javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; systemId: file:/tmp/sample.fo; lineNumber: 1; columnNumber: 39; Content is not allowed in prolog.
        at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:502)
        at org.apache.fop.cli.InputHandler.transformTo(InputHandler.java:293)
        ... 3 more
Caused by: org.xml.sax.SAXParseException; systemId: file:/tmp/sample.fo; lineNumber: 1; columnNumber: 39; Content is not allowed in prolog.
        at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
        at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
        at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:485)
        ... 4 more

为了完整起见,FO文件是由Saxon生成的,PDF尝试使用fop-2.2:

$ fop -version
FOP Version 2.2
$ fop -fo sample.fo -pdf sample.pdf
[Fatal Error] sample.fo:1:39: Content is not allowed in prolog.
...

1
任何XML文档在顶层只能有处理指令、注释和一个单一的根元素,而你的文件在文档节点下有一个文本节点,其子节点为“Test m...”,这是不允许的,也是XML解析器抱怨的原因。我们需要查看你的XSLT和输入XML以确定原因,可能是你有类似内置模板的模板将文本复制到结果中,而你并不想要这样做。 - Martin Honnen
谢谢 - 我没有包括XSL部分,因为它真的非常庞大,涉及到整个XDocs的内容。我甚至不能100%确定当所有东西都放在一起时最终会是什么样子。我需要花时间去弄清楚。 - user9645
1个回答

7

在@MartinHonnen已经友好地评论的基础上进行阐述...

错误信息为,

Content is not allowed in prolog.

这是因为XML prolog,即XML文档中根元素之前的所有内容,包含不允许的文本内容。该错误未必发生在XML声明之前。

具体而言,XML中的prolog是在XML document的上下文中定义的:

[1] document      ::= prolog element Misc*

请注意,在XML文档中,prologelement之前,它是单一的根元素。
大多数答案集中于存在文本(可见或不可见)在prolog之前,即XML声明之前的问题,但请注意,非空格文本也不能出现在prolog内部或之后的任何位置:
[22] prolog      ::= XMLDecl? Misc* (doctypedecl Misc*)?
[23] XMLDecl     ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
[24] VersionInfo ::= S 'version' Eq ("'" VersionNum "'" | '"' VersionNum '"')
[25] Eq          ::= S? '=' S?
[26] VersionNum  ::= '1.' [0-9]+
[27] Misc        ::= Comment | PI | S

在您的情况下,您有在 XML 声明(XMLDecl)和根元素(element)之间出现的 Test material... 文本内容。注释、处理指令或空格可以出现在那里,但不能是文本。

谢谢 - 我之前并没有明白"prolog"实际上是在xml标签之后。 - user9645
XML的“prolog”是指XML文档中根元素之前的所有内容。 - kjhughes
1
值得注意的是,尽管该文件不是一个格式良好的文档实体,但它是一个格式良好的外部实体,因此可以使用实体引用将其合并到另一个文档中。这就是为什么XSLT规范允许生成此类文件作为转换的输出的原因。 - Michael Kay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接