使用 DTD 时未绑定元素 "xi:include" 的前缀 "xi"

The prefix "xi" for element "xi:include" is not bound while using DTD

我想使用 xincludes 减小 xml 文件的大小,该文件在 java 8 应用程序中已增长到 21K+ LOC,但 运行 出现以下错误:

Nov 04, 2019 4:27:44 PM com.s3.config.XMLValidatorErrorHandler fatalError
SEVERE: Could not parse file (/path/to/CS_Config.xml). line: 80, The prefix "xi" for element "xi:include" is not bound.

我们目前正在使用 DTD 进行 XML 验证,虽然我已经读到模式是一个更好的长期解决方案,但我想同时获得一些工作,因为我不确定转换为架构文档需要多长时间。

这是我们主要配置的第 80 行,我在其中添加了包含:

<xi:include href="ParametersModbus.xml" />
<xi:include href="Parameters.xml" />
<xi:include href="ParametersVirtual.xml" />

到目前为止,我已将此添加到我们的 DTD 中:

<!ELEMENT xi:include (#PCDATA)>
<!ATTLIST xi:include 
    href CDATA #IMPLIED
>

<!ELEMENT Device (xi:include*,Peripheral*,VirtualPeripheral*,Camera*,MachineSync?,MachineChangeDetection?,Buffers?,Expressions?)>

我在 xinclude 上阅读其他 post 时确实有这两个模组:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setXIncludeAware(true);
dbf.setNamespaceAware(true);

不确定我是否需要添加 xpointer 引用,但有人可以提供建议。

这里有一些相关的post: XML Namespaces and DTD validation Default support for xinclude in Java 6?

11/5 更新 下面 imhotap 的回答解决了我的直接问题,当验证关闭时,我们能够处理包含。然而,当我打开验证时,我得到了这个行为:

Nov 05, 2019 3:23:42 PM com.s3.config.XMLValidatorErrorHandler error
SEVERE: Could not parse XML file (/home/jchan/eclipse-workspace/filtec-src/src/gui/test_configs/FIL0000/CS_Config.xml). line: 1, Document is invalid: no grammar found.
Nov 05, 2019 3:23:42 PM com.s3.config.XMLValidatorErrorHandler error
SEVERE: Could not parse XML file (/home/jchan/eclipse-workspace/filtec-src/src/gui/test_configs/FIL0000/CS_Config.xml). line: 1, Document root element "Peripheral", must match DOCTYPE root "null". 

我需要做些什么才能不需要在 DTD 中定义根元素吗?

11/5 更新 2 看起来我的每个包含片段只需要在被包含文件的根元素之前有 DOCTYPE 标记。所以在我的例子中是:

<?xml version="1.0"?>
<!DOCTYPE VirtualPeripheral SYSTEM "../../../architecture/peripheral-description/CS_Config.dtd">

即使将 "xi:include" 声明为元素,您仍必须将 "xi" 命名空间绑定到 XInclude 命名空间 URI。尝试在 "xi:include" 元素上将 "xmlns:xi" 声明为 #FIXED 属性,如下所示:

<!ELEMENT xi:include EMPTY>
<!ATTLIST xi:include 
  href CDATA #IMPLIED
  xmlns:xi CDATA #FIXED "http://www.w3.org/2001/XInclude">

不确定为什么您认为 XML 模式是比您的应用程序的普通 SGML/XML DTD 更好的长期解决方案。如果有的话,XML Schema 更复杂和冗长,并且实现更少;然而,它的一些功能,例如替代组,却受到了积极的警告。同样,您在示例中使用 XInclude 的功能(包含来自其他文件的片段)可以使用普通 SGML/XML entity references 更容易地实现(请参阅例如 Include one XML within another XML and parse it with python).