如何为 W3C 标记验证服务引用 XML 中的 XSD?

How to reference XSD in XML for W3C Markup Validation Service?

我想创建一个遵循 XML 模式的 XML http://www.oid-info.com/oid.xsd . This XSD should be somehow referenced in the XML file, so that a tool like the https://validator.w3.org/ 可以检查它以发现数据语义中的错误,例如当电子邮件地址无效时。

首先,我这样试过:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<oid-database xmlns="http://www.oid-info.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.oid-info.com/oid.xsd">
<submitter>
....
</submitter>
<oid>
....
</oid>
</oid-database>

W3C Markup Validation Service 说:

No DOCTYPE found! Checking XML syntax only. The DOCTYPE Declaration was not recognized or is missing. This probably means that the Formal Public Identifier contains a spelling error, or that the Declaration is not using correct syntax, or that your XML document is not using a DOCTYPE Declaration. Validation of the document has been skipped, and a simple check of the well-formedness of the XML syntax has been performed instead.

好的,所以我尝试使用 DOCTYPE

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE oid-database>
<oid-database xmlns="http://www.oid-info.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.oid-info.com/oid.xsd">
<submitter>
....
</submitter>
<oid>
....
</oid>
</oid-database>

现在 W3C 验证器说:

Line 2, Column 23: no internal or external document type declaration subset; will parse without validation

我确实知道 DTD 是什么,并且我过去使用过 DTD。我知道这里缺少 DTD。但我想检查 XSD,而不是 DTD。

我需要做什么才能让它发挥作用?

W3C Validator 将检查格式是否正确并根据 DTD 进行验证;它不会针对 XSD 进行验证。您将不得不使用另一个在线或离线验证器。

此外,您的 XML 文件应该如何提示它应该根据 OID XSD:

进行验证
<?xml version="1.0" encoding="UTF-8" ?>
<oid-database xmlns="http://oid-info.com"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://oid-info.com 
                                  http://www.oid-info.com/oid.xsd">
  <submitter>
    ....
  </submitter>
  <oid>
    ....
  </oid>
</oid-database>

另请参阅:How to link XML to XSD using schemaLocation or noNamespaceSchemaLocation?

请注意,那些 XSD 似乎有它们自己的问题,包括:

[Warning] xhtml-light.xsd:8:36: schema_reference.4: Failed to read schema document 'xml.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not .

[Error] oid.xsd:91:46: InvalidRegex: Pattern value '{?\s*(((([a-z][a-zA-Z0-9-]\s((\s*(0|[1-9][0-9])\s))?|(0|[1-9][0-9]))){2,}\s)|((0|[1-9][0-9])(.(0|[1-9][0-9]))))\s}?' is not a valid regular expression.