Error: White spaces are required between publicId and systemId

Error: White spaces are required between publicId and systemId

我想我在 XML 文件的 DOCTYPE 行中犯了错误。我该如何解决我的问题?

我使用 DOM 库来解析 XML 文件。我的 XML 文件名为 firstTime.xml:

<?xml version="1.0"?>
    <!DOCTYPE tests PUBLIC "firstTime.xsd">
    <tests xmlns="http://www.example.com/Report"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.example.com/Report firstTime.xsd">
        <test id="1">
            <coverage>65</coverage>
            <usedFramework>Junit4</usedFramework>
            <typeTest>Integration</typeTest>
        </test>
        <test id="2">
            <coverage>35</coverage>
            <usedFramework>Junit5</usedFramework>
            <typeTest>Module</typeTest>
        </test>
        <test id="3">
            <coverage>45</coverage>
            <usedFramework>Mockito</usedFramework>
            <typeTest>Integration</typeTest>
        </test>
    </tests>

我的 XSD 文件名为 firstTime.xsd:

<?xml version="1.0"?>
<xs:schema targetNamespace="http://www.example.com/Report"
           xmlns="http://www.example.com/Report"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">

    <xs:element name="tests">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="test" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="coverage" type="xs:positiveInteger"/>
                            <xs:element name="usedFramework" type="xs:string"/>
                            <xs:element name="typeTest" type="xs:string"/>
                        </xs:sequence>
                        <xs:attribute name="id" type="xs:positiveInteger"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

我的代码使用 Java:

try {
            File inputXml = new File(xmlPath);
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
            factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
            factory.setValidating(true);
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(inputXml);
        

        }catch (ParserConfigurationException | IOException | SAXException e){
            Logger.getLogger(XmlReader.class.getName()).log(Level.INFO,e.getMessage());
        }

当我尝试 运行 java app:

时发生错误

在我通过添加此行更改 xml 文件后

<!DOCTYPE tests SYSTEM "firstTime.xsd">
<!DOCTYPE tests PUBLIC "1" "firstTime.xsd">

我收到这个错误

使用PUBLIC,你需要两个字符串:

ExternalID ::= 'SYSTEM' S SystemLiteral
             | 'PUBLIC' S PubidLiteral S SystemLiteral

参见the XML Spec

替代解决方案:

  1. 强烈建议:删除DOCTYPE行;它很少与 XSD.

    一起使用
  2. DOCTYPE行修改为SYSTEM,并引用一个DTD:

    <!DOCTYPE tests SYSTEM "firstTime.dtd">
    
  3. 修复 DOCTYPE 行以包含 publicId 和 systemId,并引用 DTD:

    <!DOCTYPE tests PUBLIC "your-public-id-here" "firstTime.dtd">
    

另见

  • How to link XML to XSD using schemaLocation or noNamespaceSchemaLocation?
  • XSD schemaLocation, targetNamespace, default XML namespace matching