概念 规定xml文档的书写规则
* 作为框架的使用者(程序员)
1.能够在xml中引入约束文档
2.能够简单的读懂约束文档
逻辑图 以下是约束文档的的作用和逻辑关系的图解:
分类 DTD 一种简单的约束文档
student.dtd 1 2 3 4 5 6 <!ELEMENT students (student *) > <!ELEMENT student (name ,age ,sex )> <!ELEMENT name (#PCDATA )> <!ELEMENT age (#PCDATA )> <!ELEMENT sex (#PCDATA )> <!ATTLIST student number ID #REQUIRED >
这是对于约束文档的说明:
引入约束文档 1.内部dtd文档
将约束规则定义在xml文档中(不常用)
<!DOCTYPE students [
里面书写约束文档
]>
2.外部dtd文档
将约束文档定义在外部dtd文件中
1.本地的
<!DOCTYPE 根标签 SYSTEM "外部的dtd文件">
2.网络的
<!DOCTYPE 根标签 PUBLIC "dtd文件的名字" "外部的dtd文件URL">
dtd实例 根据上面的约束文档书写的xml文档
1 2 3 4 5 6 7 8 9 10 11 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE students SYSTEM "student.dtd" > <students > <student number ="itcast_0001" > <name > tom</name > <age > 18</age > <sex > male</sex > </student > </students >
Schema 一种复杂的约束技术
弥补了dtd约束文档的缺陷(dtd定义的标签里面的值不能进行限制)。
student.xsd 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 <?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" > <xsd:element name ="students" type ="studentsType" /> <xsd:complexType name ="studentsType" > <xsd:sequence > <xsd:element name ="student" type ="studentType" minOccurs ="0" maxOccurs ="unbounded" /> </xsd:sequence > </xsd:complexType > <xsd:complexType name ="studentType" > <xsd:sequence > <xsd:element name ="name" type ="xsd:string" /> <xsd:element name ="age" type ="ageType" /> <xsd:element name ="sex" type ="sexType" /> </xsd:sequence > <xsd:attribute name ="number" type ="numberType" use ="required" /> </xsd:complexType > <xsd:simpleType name ="sexType" > <xsd:restriction base ="xsd:string" > <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" > <xsd:pattern value ="xml_\d{4}" /> </xsd:restriction > </xsd:simpleType > </xsd:schema >
引入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"
实例 这是一个只有一个约束文档的实例
1 2 3 4 5 6 7 8 9 10 11 12 13 <?xml version="1.0" encoding="UTF-8" ?> <students xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns ="http://www.itcast.cn/xml" xsi:schemaLocation ="http://www.itcast.cn/xml student.xsd" > <student number ="heima_0001" > <name > tom</name > <age > 18</age > <sex > male</sex > </student > </students >
以下是多个约束文档的实例(spring框架的配置文件)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 <?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: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 >
<
来看看XML到底是个啥--xml解析
来看看XML到底是个啥--xml快速入门
>