约束,规定xml文档的书写规则
作为框架的使用者(程序员):
(1)能够在 xml 中引入约束文档
(2)能够简单的读懂约束文档
分类:
- DTD:一种简单的约束技术
- Schema:一种复杂的约束技术
一、DID,一种简单的约束技术
引入dtd文档到xml文档中
* 内部dtd:将约束规则定义在xml文档中
* 外部dtd:将约束的规则定义在外部的dtd文件中
* 本地:<!DOCTYPE 根标签名 SYSTEM "dtd文件的位置">
* 网络:<!DOCTYPE 根标签名 PUBLIC "dtd文件名字" "dtd文件的位置URL">
DTD 文档student.dtd
示例:
<!--跟标签是 students,跟标签里 student 可以一次或多次-->
<!ELEMENT students (student*) >
<!--student 里的标签是 name,age,sex-->
<!ELEMENT student (name,age,sex),并且需要按照顺序出现>
<!--name,age,sex 三个元素均为字符串-->
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT sex (#PCDATA)>
<!--为student标签声明属性,且属性名唯一,也必须要出现-->
<!ATTLIST student number ID #REQUIRED>
按照此 DTD 文档编写的 xml 文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!--将约束的规则定义在外部的dtd文件中-->
<!DOCTYPE students SYSTEM "student.dtd">
<students>
<student number="itcast_0001">
<name>tom</name>
<age>18</age>
<sex>male</sex>
</student>
</students>
二、schema,一种复杂的约束技术
引入:
(1)填写xml文档的根元素
(2)引入xsi前缀. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
(3)引入xsd文件命名空间. xsi:schemaLocation="http://www.itcast.cn/xml student.xsd"
(4)为每一个xsd约束声明一个前缀,作为标识 xmlns="http://www.itcast.cn/xml"
schema 文档student.xsd
示例:
<?xml version="1.0"?>
<xsd:schema xmlns="http://www.itcast.cn/xml"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.itcast.cn/xml" elementFormDefault="qualified">
<!--定义一个元素,students,类型为 studentsType(自定义的类型,下面需要声明)-->
<xsd:element name="students" type="studentsType"/>
<!--声明 studentsType 类型-->
<xsd:complexType name="studentsType">
<!--按顺序出现,声明 student 元素,也是自定义类型 studentType-->
<xsd:sequence>
<!--minOccurs最少出现0次,maxOccurs最多出现没有绑定-->
<xsd:element name="student" type="studentType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<!--声明 studentType 类型-->
<xsd:complexType name="studentType">
<xsd:sequence>
<!--元素 name,age,sex按顺序出现-->
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="age" type="ageType" />
<xsd:element name="sex" type="sexType" />
</xsd:sequence>
<!--定义属性 number-->
<xsd:attribute name="number" type="numberType" use="required"/>
</xsd:complexType>
<xsd:simpleType name="sexType">
<xsd:restriction base="xsd:string">
<!--enumeration表明性别只能在其中取其一-->
<xsd:enumeration value="male"/>
<xsd:enumeration value="female"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ageType">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="256"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="numberType">
<xsd:restriction base="xsd:string">
<!--定义 numberType ,pattern表示 值必须符合下面的规则-->
<xsd:pattern value="xml_\d{4}"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
按照 student.xsd
写出的 xml 示例:
<?xml version="1.0" encoding="UTF-8" ?>
<students
<!--xmlns:xsi=XXX 表示固定格式,选择合适的版本-->
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<!--给http://w.xx 一个空前缀,这样可解使用 student.xsd-->
xmlns="http://www.itcast.cn/xml"
<!--xsi:schemaLocation 引入schema 路径,给路径student.xsd 取名为 http://w.xx-->
xsi:schemaLocation="http://w.xxx student.xsd"
>
<student number="xml_0001">
<name>tom</name>
<age>18</age>
<sex>male</sex>
</student>
</students>
如果不给 student.xsd 取名为空前缀:
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--每次使用context里的内容,都需要使用 context前缀,其他类似-->
<context:annotation-config />
<context:component-scan base-package="cn.cisol.mvcdemo">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
<entry key="htm" value="text/html" />
</map>
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
</bean>
</list>
</property>
<property name="ignoreAcceptHeader" value="true" />
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsps/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="209715200" />
<property name="defaultEncoding" value="UTF-8" />
<property name="resolveLazily" value="true" />
</bean>
</beans>