DTD、XSD(xml Schema语言)约束

本文介绍XML的两种约束技术:DTD和Schema。详细讲解了如何通过.dtd和.xsd文件进行约束定义,并演示了MyBatis和Spring框架中如何应用这些约束来提高XML文件的编写效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录:DTD、Schema约束

dtd和Schema约束:http://blog.csdn.net/wust__wangfan/article/details/53187982

  • DTD约束:
    1. dtd约束文件概念:DTD约束技术
    2. 编写dtd约束:.dtd文件:https://jingyan.baidu.com/article/a3a3f8113ebb168da2eb8adb.html
    3. 导入dtd约束的三种方式
      1. 内部导入
      2. 外部导入
      3. 互联网导入
    4. xml文件导入dtd约束的好处:1.编写出错会有提示;2.会有提示可用的标签有哪些
    5. 本地引入:
      实例:mybatis引入dtd约束:
      MyBatis的约束文件位置,都放在jar包里面,先添加mybatis依赖,刷新项目后可看到:

      MyBatis 主配置文件 mybatis.xml:官网文档也有实例
      将上面两个文件复制保存在本地。
      然后

    6. 在线引入:
      <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
      <configuration>
      	<!-- 这里写配置内容 -->
      </configuration>

      MyBatis 映射文件 mapper.xml

      <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
      <mapper namespace="命名空间">
      	<!-- SQL语句 -->
      </mapper>

       

  • Schema约束
    1. Schema约束概念
    2. 编写Schema约束:.xsd文件
    3. 导入Schema约束的方式
    4. xml文件导入schema约束的好处:1.编写出错会有提示;2.会有提示可用的标签有哪些
    5. 实例:spring引入schema约束:以下四行是spring.xml文件的基石

      spring约束大全:application.xml
      <?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:aop="http://www.springframework.org/schema/aop"
             xmlns:tx="http://www.springframework.org/schema/tx"
      
          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/aop
          http://www.springframework.org/schema/aop/spring-aop.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx.xsd">
      
      
      </beans>
    6. 练习1:在xml文件中添加spring  aop 约束
      1. 第一步:项目添加spring-aop.jar包
      2. 第二步:在此jar下找到配置文件 - .xsd文件
      3. 第三步:打开spring-aop-2.0.xsd
      4. 第四步:在xml文件中添加3行
      5. 完成,可以使用spring aop提示了
    7. 练习2:在xml文件中添加 dubbo 约束(spring和dubbo整合,所以也是在sprign.xml文件中配置)
      1. 第一种:在线引用
        <?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:dubbo="http://code.alibabatech.com/schema/dubbo"
        	xsi:schemaLocation=
        	"http://www.springframework.org/schema/beans 
        	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
        	http://code.alibabatech.com/schema/dubbo 
        	http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
        	
        	
        </beans>
      2. 第二种:本地引用
        1. 第一步:项目添加dubbo依赖
          <dependency>
          		<groupId>com.alibaba</groupId>
          		<artifactId>dubbo</artifactId>
          		<version>2.6.0</version>
          </dependency>
        2. 第二步:刷新项目后,查看jar所在位置如图所示
        3. 第三步:解压dubbo-2.8.4.jar,从meta-inf中找到dubbo.xsd
        4. 第四步:Eclipse中Window =>preferencr => xml => xml catalog添加catalog,如图所示,Location指向本机dubbo.xsd文件, Key是http://code.alibabatech.com/schema/dubbo/dubbo.xsd,key是从xml schemaLocation拷贝过来的。
        5. 第五步:在src\main\java下创建applicationContext.xml
          <?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:dubbo="http://code.alibabatech.com/schema/dubbo"
          	xsi:schemaLocation="http://www.springframework.org/schema/beans 
          			http://www.springframework.org/schema/beans/spring-beans.xsd 
          			http://code.alibabatech.com/schema/dubbo 
          			http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">
          
          
          
          </beans>

           

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值