为了验证 xml,我们需要 dtd 或 xsd。如果我们想根据 dtd 验证 xml,那么我们需要编写这个。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
当我们的 dtd 存在于文件系统中时,使用 SYSTEM;如果我们的 dtd 存在于某个网络中,则使用 PUBLIC。在这种情况下,如果我们正在写这个
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd"意味着我们的 dtd 存在于具有此地址的网络中,并且我们的 hibernate-configuration xml 文件将根据此 dtd 进行验证。
但是为什么要写这个??????
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
所以请回答我的问题。
最佳答案
DOCTYPE 中的值 -//Hibernate/Hibernate Configuration DTD 3.0//EN
是 Formal Public Identifier 。它唯一标识PUBLIC
external entity ,并且可用于解析 URI 引用。
In addition to a system identifier, an external identifier may include a public identifier.
An XML processor attempting to retrieve the entity's content may use any combination of the public and system identifiers as well as additional information outside the scope of this specification to try to generate an alternative URI reference. If the processor is unable to do so, it must use the URI reference specified in the system literal. Before a match is attempted, all strings of white space in the public identifier must be normalized to single space characters (#x20), and leading and trailing white space must be removed.
此外,还有一个很好的引用资料解释了 FPI 的组成部分:https://www.quackit.com/xml/tutorial/dtd_fpi.cfm
Here's a real life example of an FPI. In this case, the DTD was created by the W3C for XHTML:
-//W3C//DTD XHTML 1.0 Transitional//EN
An FPI is made up of 4 fields, each separated by double forward slashes (
//
):
-
Indicates whether the DTD is connected to a formal standard or not. If the DTD hasn't been approved (for example, you've defined the DTD yourself), use a hypen (-). If the DTD has been approved by a nonstandards body, use a plus sign "+". If the DTD has been approved by a formal standards body this field should be a reference to the standard itself.
W3C
Holds the name of the group (or person) responsible for the DTD. The above example is maintained by the W3C, so "W3C" appears in the second field.
DTD XHTML 1.0 Transitional
Indicates the type of document that is being described. This usually contains some form of unique identifier (such as a version number).
EN
Specifies the language that the DTD uses. This is achieved by using the two letter identifier for the language (i.e. for english, use "EN").
关于java - 为什么我们要在 Hibernate 配置或映射文件中写入 <!DOCTYPE> 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50202033/