XML约束之DTD约束 ----- java进阶篇

目录

一、xml语法与xml约束

二、xml约束技术

三、DTD约束

3.1、简介

3.2、内部的 DOCTYPE 声明       

3.3、外部导入

3.4、xml文件内容

3.5、dtd文件内容   类似于一个css文件

3.6、使用DTD的好处

3.7、相关概念

3.7.1、元素

3.7.2、属性

3.7.3、实体

3.7.4、PCDATA(被解析的字符数据(parsed character data))

3.7.5、CDATA(字符数据(character data))

3.8、语法(会看级别)

3.8.1、约束标签

3.9、 类别

3.10、元素内容

3.10.1、 顺序问题

3.10.2、次数问题

3.10.3、约束属性

3.10.4、默认值

3.10.5、属性类型:控制属性值的

四、Schema约束

五、总结


能看懂约束内容,能根据约束内容写出符合规则的xml文件

一、xml语法与xml约束

  1. xml语法:规范xml文件的基本编写规则。(由w3c组织制定)
  2. xml约束:规范xml文件的数据内容格式的编写规则(由开发者自行定义)

二、xml约束技术

DTD约束:语法相对简单,功能也相对简单,学习成本低

Schema约束:语法相对复杂,功能也相对强大,学习成本也相对高

三、DTD约束

3.1、简介

文档类型定义(DTD)可定义合法的XML文档构建模块。它使用一系列合法的元素来定义文档的结构。

DTD 可被成行地声明于 XML 文档中,也可作为一个外部引用。

 

3.2、内部的 DOCTYPE 声明       <!DOCTYPE 根元素 [元素声明]>

<?xml version="1.0"?>
<!DOCTYPE note [
  <!ELEMENT note (to,from,heading,body)>
  <!ELEMENT to      (#PCDATA)>
  <!ELEMENT from    (#PCDATA)>
  <!ELEMENT heading (#PCDATA)>
  <!ELEMENT body    (#PCDATA)>
]>
<note>
  <to>George</to>
  <from>John</from>
  <heading>Reminder</heading>
  <body>Don't forget the meeting!</body>
</note>
<!--
带有 DTD 的 XML 文档实例(请在 IE5 以及更高的版本打开,并选择查看源代码)
!DOCTYPE note (第二行)定义此文档是 note 类型的文档。

!ELEMENT note (第三行)定义 note 元素有四个元素:"to、from、heading,、body"

!ELEMENT to (第四行)定义 to 元素为 "#PCDATA" 类型

!ELEMENT from (第五行)定义 frome 元素为 "#PCDATA" 类型

!ELEMENT heading (第六行)定义 heading 元素为 "#PCDATA" 类型

!ELEMENT body (第七行)定义 body 元素为 "#PCDATA" 类型


--->

3.3、外部导入

 

本地文件系统:<!DOCTYPE 根元素 SYSTEM "文件名">  文件在本地

公共的外部导入:<!DOCTYPE 根元素 public "网络上的dtd文件">  别人写好的文件

 

3.4、xml文件内容

<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note> 

3.5、dtd文件内容   类似于一个css文件

<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

3.6、使用DTD的好处

可以定义XML文本的格式

3.7、相关概念

3.7.1、元素

相当于HTML中的标签,但xml中标签(元素)可以自行定义

 

3.7.2、属性

提供元素的额外信息,如:

<student id="002">

 

3.7.3、实体

实体是用来定义普通文本的变量。

实体引用字符
&lt;<
&gt;>
&amp;&
&quot;"
&apos;'

 

3.7.4、PCDATA(被解析的字符数据(parsed character data))

可把字符数据想象为 XML 元素的开始标签与结束标签之间的文本。会被解析器解析的文本这些文本将被解析器检查实体以及标记

 

3.7.5、CDATA(字符数据(character data))

不会被解析器解析的文本在这些文本中的标签不会被当作标记来对待,其中的实体也不会被展开

 

3.8、语法(会看级别)

3.8.1、约束标签

                               <!ELEMENT 元素名称 类别>  或 <!ELEMENT 元素名称 ()>

3.9、 类别

         空标签: EMPTY。 表示元素一定是空元素

         普通字符串: (#PCDATA)。表示元素的内容一定是普通字符串(不能含有子标签)。

         任何内容: ANY。表示元素的内容可以是任意内容(包括子标签)

<?xml version="1.0"?>
<!DOCTYPE note [
  <!ELEMENT note (to,from,heading,body)>
  <!ELEMENT to      (#PCDATA)>
  <!ELEMENT from          ANY>
  <!ELEMENT heading       EMPTY>
  <!ELEMENT body    (#PCDATA)>
]>
<note>
  <to>George</to>
  <from>John</from>
  <heading></heading>
  <body>Don't forget the meeting!</body>
</note>

3.10、元素内容

3.10.1、 顺序问题

          <!ELEMENT 元素名称 (子元素名称 1,子元素名称 2,.....)>: 按顺序出现子标签

<!DOCTYPE note [<!ELEMENT note (to,from,heading,body)>
<!--出现一次,且必须出现一次-->

3.10.2、次数问题

                       标签   :  必须且只出现1次。

                       标签+  : 至少出现1次

                       标签*   : 0或n次。

                       标签?  : 0 或1次。


<?xml version="1.0"?>
<!DOCTYPE note [
  <!ELEMENT note (to,from,heading,body+)>
  <!ELEMENT to      (#PCDATA)>
  <!ELEMENT from          ANY>
  <!ELEMENT heading       EMPTY>
  <!ELEMENT body    (#PCDATA)>
]>
<note>
  <to>George</to>
  <from>John</from>
  <heading></heading>
  <body>Don't forget the meeting!</body>
  <body>Don't forget the meeting!</body>
  <body>Don't forget the meeting!</body>
</note>
<!--出现一次或多次,这个语法与正则表达式相似-->

3.10.3、约束属性

          <!ATTLIST 元素名称 属性名称 属性类型 默认值>

3.10.4、默认值

<?xml version="1.0"?>
<!DOCTYPE note [
  <!ELEMENT note (to,from,heading,body+)>
  <!ELEMENT to      (#PCDATA)>
  <!ELEMENT from          ANY>
  <!ELEMENT heading       EMPTY>
  <!ELEMENT body    (#PCDATA)>

<!--<!ATTLIST 元素名称 属性名称 属性类型 默认值>-->
<!--#REQUIRED 属性值是必需的 -->
<!--<!ATTLIST to name="Jack" CDATA #REQUIRED>-->
<!--#IMPLIED   属性不是必需的-->
<!--<!ATTLIST to name="Jack" CDATA #IMPLIED>-->
<!--属性不是必须的,但属性值是固定的-->
 <!ATTLIST to name CDATA #FIXED "Jack">
]>
<note>
  <!--<to name="Jack">George</to>-->
  <!--<to>George</to>-->
  <to name="Jack">George</to>
  <from>John</from>
  <heading></heading>
  <body>Don't forget the meeting!</body>
  <body>Don't forget the meeting!</body>
  <body>Don't forget the meeting!</body>
</note>

3.10.5、属性类型:控制属性值的

<?xml version="1.0"?>
<!DOCTYPE note [
  <!ELEMENT note (to,from,heading,body+)>
  <!ELEMENT to      (#PCDATA)>
  <!ELEMENT from          ANY>
  <!ELEMENT heading       EMPTY>
  <!ELEMENT body    (#PCDATA)>
<!--<!ATTLIST 元素名称 属性名称 属性类型 默认值>-->

<!--CDATA :表示普通字符串-->
<!-- <!ATTLIST to name  CDATA #FIXED "Jack">-->

<!--(en1|en2|..): 表示一定是任选其中的一个值-->
 <!--<!ATTLIST to name (a|b|c)  #REQUIRED>-->

<!--ID:表示在一个xml文档中该属性值必须唯一。值不能以数字开头-->
<!ATTLIST to  id ID  #REQUIRED>



]>
<note>
   <!--<to name="Jack">George</to>-->
   <!--<to name="a">George</to>-->
    <to id="a1">George</to>
    <to id="a2">George</to>
    <to id="a3">George</to>
 <from>John</from>
  <heading></heading>
  <body>Don't forget the meeting!</body>
  <body>Don't forget the meeting!</body>
  <body>Don't forget the meeting!</body>
</note>

四、Schema约束

名称空间:告诉xml文档的哪个元素被哪个schema文档约束 在一个xml文档中,不同的标签可以受到不同的schema文档的约束。

  •  一个名称空间受到schema文档约束的情况
  • 多个名称空间受到多个schema文档约束的情况
  • 默认名称空间的情况
<书架 xmlns="http://www.it315.org/xmlbook/schema"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation=“http://www.it315.org/xmlbook/schema  book.xsd">
		<书>
		<书名>JavaScript网页开发</书名>
		<作者>张孝祥</作者>
		<售价>28.00元</售价>
		</书>
	<书架>
  • 没有名称空间的情况
<?xml version="1.0" encoding="UTF-8"?>
<书架 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="xmlbook.xsd">
	<书>
		<书名>JavaScript网页开发</书名>
		<作者>张孝祥</作者>
		<售价>28.00元</售价>
	</书>
</书架>

book.xml

<?xml version="1.0" encoding="UTF-8"?>
<itcast:书架 xmlns:itcast="http://www.itcast.cn"
				xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
				xsi:schemaLocation="http://www.itcast.cn book.xsd">
	<itcast:书>
		<itcast:书名>JavaScript网页开发</itcast:书名>
		<itcast:作者>张孝祥</itcast:作者>
		<itcast:售价>28</itcast:售价>
	</itcast:书>

</itcast:书架>

book.xsd

<?xml version="1.0" encoding="UTF-8" ?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
					  targetNamespace="http://www.itcast.cn"
					  elementFormDefault="qualified">
	<xs:element name='书架' >
		<xs:complexType>
			<xs:sequence maxOccurs='unbounded' >
				<xs:element name='书' >
					<xs:complexType>
						<xs:sequence>
							<xs:element name='书名' type='xs:string' />
							<xs:element name='作者' type='xs:string' />
							<xs:element name='书架' type='xs:integer' />
						</xs:sequence>
					</xs:complexType>
				</xs:element>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

五、总结

声明名称空间:xmlns

URIUniform Resource Identifier,统一资源标识符):表示名称空间

targetNamespace元素:指定schema文档中声明的元素的名称空间

elementFormDefault元素:用于指定,该schema文档中声明的根元素及其所有子元素都属于targetNamespace所指定的名称空间

xsi:w3c指定的名称空间

 

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    ">
    
    <!-- 在Spring上下文中载入property文件信息 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 数据访问层配置 -->
    <import resource="classpath:/dao.xml" />
    <!-- 服务层配置 -->
    <import resource="classpath:/service.xml" />
    
    <!-- 自动扫描@Controller标注的类控制器类 -->
	<context:component-scan base-package="com.toceansoft.web" />


</beans>
  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值