Xml 简明教程
XML - Validation
Validation 是一个验证 XML 文档的过程。如果 XML 文档的内容与其元素、属性和关联的文档类型声明 (DTD) 相匹配,并且该文档符合其中表达的约束,则该文档被称为有效的。XML 解析器以两种方式处理验证。它们为:
Validation is a process by which an XML document is validated. An XML document is said to be valid if its contents match with the elements, attributes and associated document type declaration(DTD), and if the document complies with the constraints expressed in it. Validation is dealt in two ways by the XML parser. They are −
-
Well-formed XML document
-
Valid XML document
Well-formed XML Document
如果 XML 文档遵守以下规则,则该文档称为 well-formed :
An XML document is said to be well-formed if it adheres to the following rules −
-
Non DTD XML files must use the predefined character entities for amp(&), apos(single quote), gt(>), lt(<), quot(double quote).
-
It must follow the ordering of the tag. i.e., the inner tag must be closed before closing the outer tag.
-
Each of its opening tags must have a closing tag or it must be a self ending tag.(<title>….</title> or <title/>).
-
It must have only one attribute in a start tag, which needs to be quoted.
-
amp(&), apos(single quote), gt(>), lt(<), quot(double quote) entities other than these must be declared.
Example
以下是良好格式 XML 文档的示例 −
Following is an example of a well-formed XML document −
<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>
<!DOCTYPE address
[
<!ELEMENT address (name,company,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
]>
<address>
<name>Tanmay Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 123-4567</phone>
</address>
上述示例被称之为良好格式,原因如下 −
The above example is said to be well-formed as −
-
It defines the type of document. Here, the document type is element type.
-
It includes a root element named as address.
-
Each of the child elements among name, company and phone is enclosed in its self explanatory tag.
-
Order of the tags is maintained.
Valid XML Document
如果 XML 文档已正确格式化并关联有文档类型声明 (DTD),则称之为有效的 XML 文档。我们将在 XML - DTDs 章节中详细学习 DTD。
If an XML document is well-formed and has an associated Document Type Declaration (DTD), then it is said to be a valid XML document. We will study more about DTD in the chapter XML - DTDs.