XML声明之前,“在序言中不允许内容”错误但一无所获

时间:2018-07-24 14:07:21

标签: xml xml-parsing saxon apache-fop

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

这些似乎都可以归结为两点:

  1. <?xml?>开头之前有一个或多个字符(可能不可见)。
  2. 主体中有些字节序列不适合encoding标记中定义的<?xml?>

关于#1,我用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.

关于#2,我检查了file

$ file sample.fo
sample.fo: XML 1.0 document, UTF-8 Unicode text, with very long lines

我只能想到Material Icons字体代码点的两个实例,它们是3字节UTF-8字符,并且似乎已正确编码为verified online with this site

  1. 图标“ keyboard_arrow_down”是代码点e313,其编码为ee 8c 93
  2. 图标“回复”是编码为ee 85 9e的代码点e15e

如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 个答案:

答案 0 :(得分:0)

详细介绍@MartinHonnen已经帮助commented ...

错误

  

序言中不允许包含内容。

之所以会出现错误,是因为 XML序言 具有 XML文档中根元素之前的所有内容 不允许的文字内容。在XML声明之前 不一定必须发生该错误。

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

[1] document      ::= prolog element Misc*

请注意,prolog在XML文档的单个根元素element之前。

大多数答案集中在XML声明之前prolog开头有文本(可见或不可见)的问题,但请注意,非空白文本不能出现在内或后的任何地方 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声明(Test material...)和根元素(XMLDecl)之间出现element个文本内容。注释,处理指令或空白可以出现在此处,但文本不能出现。