XML 第二篇(约束)

1.约束的概念:

        规定xml文档的书写规则

        作为框架的使用者(程序员):

                1.能够在xml文件中引入约束文档

                2.能够简单的读懂约束文档

        分类:

                1.DTD:一中简单的约束技术

                2.Schema:一种复杂的约束技术

2.DTD约束:

        引入dtd文档到xml文档中

                内部dtd:将约束规则定义在xml文档中

                外部dtd:将约束的规则定义在外部的dtd文件中

                        本地:<! DOCTYPE 根标签名 SYSTEM "dtd文件的位置">

                        网络:<! DOCTYPE 根标签名 PUBLIC "dtd文件名字" "dtd文件的位置URL">

        导入dtd的方式:

                  内部导入

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

                外部导入
                本地文件系统:            
                <!DOCTYPE note SYSTEM "note.dtd">

                公共的外部导入:
                <!DOCTYPE 根元素 PUBLIC "http://www.abyg.cn/abyg.dtd">

3.DTD语法:

        约束标签:<! ELEMENT 元素名称 类别> 或 <! ELEMENT 元素名称 (元素名称)>

        类别:

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

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

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

        元素内容:

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

                次数问题:

                        标签        :  必须且出现1次。

                        标签+      :  至少出现1次

                        标签*       :  0或n次

                        标签?       :  0 或 1次

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

                默认值:

                        #REQUIRED  属性是必须的

                        #IMPLIED     属性不是必须的

                        #FIXED value  属性不是必须的,但属性值是固定的

                属性类型:控制属性值的

                        CDATA:表示字符串

                        (en1|en2|....):表示一定是任选其中的一个值

                        ID:表示在一个xml文档中该属性值必须唯一。值不能以数字开头

4.Schema约束:

引入:

  1. 填写xml文档的根元素
  2. 引入xsi前缀:xmlns:xsi="http://www.w3.org/2001/xmlSchema-instance"
  3. 引入xsd文件命名空间:xsi:schemaLocation="http://www.company.com student.xsd"
  4. 为每一个xsd约束声明一个前缀,作为标识:xmlns="http://www.company.com"

        <students   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns="http:/www.company.com"
                xsi:schemaLocation="http://www.company.com  student.xsd">

名称空间:告诉xml文档的哪个元素被哪个schema文档约束

在一个xml文档中,不同的标签可以受到不同的schema文档的约束。

5.案例:

1.dtd:

note.dtd

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

note.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- <!DOCTYPE note SYSTEM "note.dtd">  -->

<!DOCTYPE note [
  <!ELEMENT note (to+,from*,heading?,body)>
  <!ELEMENT to      (#PCDATA)>
  <!ELEMENT from     EMPTY>
  <!ELEMENT heading  ANY>
  <!ELEMENT body    (#PCDATA)>
  <!ATTLIST to name (a|b) #REQUIRED>
]> 
<note>
	<to name="a"></to>
	<from></from>
	<heading></heading>
	<body></body>
</note>

2.Schema:

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	<!-- 配置 读取properties文件 jdbc.properties -->
	<context:property-placeholder location="classpath:jdbc.properties" />

	<!-- 配置 数据源 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<!-- 配置SqlSessionFactory -->
	<bean class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 设置MyBatis核心配置文件 -->
		<property name="configLocation" value="classpath:sqlMapConfig.xml" />
		<!-- 设置数据源 -->
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 配置Mapper扫描 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 设置Mapper扫描包 -->
		<property name="basePackage" value="cn.xclink.mapper" />
	</bean>

	<!-- 事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 传播行为 -->
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="query*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<!-- 切面 -->
	<aop:config>
		<aop:advisor advice-ref="txAdvice"
			pointcut="execution(* cn.xclink.service.*.*(..))" />
	</aop:config>

	<!-- 开启注解配置事务 -->
	<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
</beans>

student.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!-- 
	1.填写xml文档的根元素
	2.引入xsi前缀.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	3.引入xsd文件命名空间.  xsi:schemaLocation="http://www.company.com  student.xsd"
	4.为每一个xsd约束声明一个前缀,作为标识  xmlns="http://www.company.com" 
	
	
 -->
 <students   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 			 xmlns="http://www.company.com"
 		   xsi:schemaLocation="http://www.company.com  student.xsd"
 		    >
 	<student id="1001">
 		<name>tom</name>
 		<age>18</age>
 		<sex>male</sex>
 	</student>
		 
 </students>

student.xsd

<?xml version="1.0"?>
<xsd:schema xmlns="http://www.company.com"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.company.com" 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="id" 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="\d{4}"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema> 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小yu别错过

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值