XML的验证模式DTD与XSD的区别
程序员文章站
2022-05-30 12:54:37
...
xml文件的正确性是由xml的验证模式来保证的,比较常见的验证模式有两种:DTD和XSD。
1.DTD:
DTD(Document Type Definition)即文档类型定义,是一种xml约束模式语言,是xml文件的验证机制,属于xml文件组成的一部分。DTD是一种保证xml文档格式正确的有效方法,可以 通过比较xml文档和DTD文档来查看文档是否符合规范,元素和标签使用是否正确。一个DTD文档包含:元素的定义规则,元素间关系的定义规则,元素可使用的属性,可使用的实体或符号规则。
以下是spring-beans-2.0.dtd的部分内容:
<!ELEMENT beans (
description?,
(import | alias | bean)*
)>
<!ATTLIST beans default-lazy-init (true | false) "false">
<!ATTLIST beans default-autowire (no | byName | byType | constructor | autodetect) "no">
<!ATTLIST beans default-dependency-check (none | objects | simple | all) "none">
<!ATTLIST beans default-init-method CDATA #IMPLIED>
<!ATTLIST beans default-destroy-method CDATA #IMPLIED>
<!ATTLIST beans default-merge (true | false) "false">
<!ELEMENT description (#PCDATA)>
<!ELEMENT import EMPTY>
<!ATTLIST import resource CDATA #REQUIRED>
<!ELEMENT alias EMPTY>
<!ATTLIST alias name CDATA #REQUIRED>
<!ATTLIST alias alias CDATA #REQUIRED>
<!ELEMENT meta EMPTY>
<!ATTLIST meta key CDATA #REQUIRED>
<!ATTLIST meta value CDATA #REQUIRED>
...
2.XSD:
XSD(XML Schemas Definition)即xml结构定义文档。xsd描述了xml文档的结构,可以用一个指定的xml schema来验证某个xml文档,以检查该xml是否符合其要求。文档设计者可以通过xml schema指定一个xml文档所允许的结构和内容,并可据此检查一个xml文档是否是有效的。
下面是spring-beans-3.1.xsd的部分内容:
<xsd:schema xmlns="http://www.springframework.org/schema/beans" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.springframework.org/schema/beans">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace"/>
<xsd:annotation>
<xsd:documentation>
<![CDATA[
Spring XML Beans Schema, version 3.1 Authors: Juergen Hoeller, Rob Harrop, Mark Fisher, Chris Beams This defines a simple and consistent way of creating a namespace of JavaBeans objects, managed by a Spring BeanFactory, read by XmlBeanDefinitionReader (with DefaultBeanDefinitionDocumentReader). This document type is used by most Spring functionality, including web application contexts, which are based on bean factories. Each "bean" element in this document defines a JavaBean. Typically the bean class is specified, along with JavaBean properties and/or constructor arguments. A bean instance can be a "singleton" (shared instance) or a "prototype" (independent instance). Further scopes can be provided by extended bean factories, for example in a web environment. References among beans are supported, that is, setting a JavaBean property or a constructor argument to refer to another bean in the same factory (or an ancestor factory). As alternative to bean references, "inner bean definitions" can be used. Singleton flags of such inner bean definitions are effectively ignored: inner beans are typically anonymous prototypes. There is also support for lists, sets, maps, and java.util.Properties as bean property types or constructor argument types.
]]>
</xsd:documentation>
</xsd:annotation>
<!-- base types -->
<xsd:complexType name="identifiedType" abstract="true">
<xsd:annotation>
<xsd:documentation>
<![CDATA[
The unique identifier for a bean. The scope of the identifier is the enclosing bean factory.
]]>
</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="id" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
<![CDATA[
The unique identifier for a bean. A bean id may not be used more than once within the same <beans> element.
]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<!-- Top-level <beans> tag -->
<xsd:element name="beans">
3.主要区别:
DTD需要在xml文件的头部声明,例如
<!DOCTYPE beans PUBLIC "-//Spring//DTD BEAN 2.0//EN" "http://Springframework.org/dtd/Spring-beans-2.0.dtd">
而XSD通过xmlns名称空间的方式验证的,例如
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.Springframwork.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi=schemaLocation="http://www.Springframewrok.org/schema/beans
http://www.Springframework.org/schema/beans/Spring-beans.xsd"
...
</beans>
推荐阅读
-
AJAX使用get与post模式的区别分析
-
Linux UDP socket 设置为的非阻塞模式与阻塞模式区别
-
服务器授权模式每服务器同时连接数与每设备或每用户的区别小结
-
简单工厂、工厂方法、抽象工厂、策略模式、策略与工厂的区别
-
Java 单例模式设计 饿汉式与懒汉式的区别与作用
-
Net程序员讲JavaSpringBoot系列:Yml与JSON、XML的区别
-
微信公众号编辑模式与开发模式的区别介绍
-
Entity Framework 更新模式之Attach与EntityState.Modified模式的区别
-
vue-router中hash模式与history模式的区别
-
C#使用xsd文件验证XML格式是否正确的实现方法