XML约束

文章目录

  • 规定XML文档中的标签格式

DTD约束

外部DTD

  1. 内部dtd&外部dtd:内部dtd直接定义在xml文档中(不常用),外部dtd新建一个文档单独存放;
  2. 加载类型
    1. 本地外部DTD: <!DOCTYPE 名称 SYSTEM
    2. 网络外部DTD: <!DOCTYPE 名称 PUBLICE “命名空间”
  3. .DTD解析
  • 缺点: 无法约束数据类型;
    image
<!ELEMENT web-app (servlet*,servlet-mapping*,wel-file-list?)>
<!--根元素web-app下按顺序有servlet,servlet-mapping,wel-file-list三个子标签-->
<!--*代表任意个,?可存在可不存在-->

<!ELEMENT servlet (servlet-name,description?,(servlet-class|jsp-file))>
<!--(servlet-class|jsp-file)代表只能存有其中一个子标签-->

<!ELEMENT servlet-name (#PCDATA)>
<!--输入文本-->

<!ELEMENT wel-file-list (welcome-file+)>
<!--至少有一个子标签welcome-file-->

schema

  • 根标签格式
  1. 编写根标签
  2. 引用约束地址:
    xsi: schemaLocation = “默认空间 约束地址.xsd”
  • 默认空间 --> 自定义;
  • 约束地址.xsd --> schema约束文件;
  1. 引用实例名称空间:
    xmlns: xsi = “http://www.w3.org/2001/XMLSchema-instance
  • 固定地址;
  1. 默认名称空间
  • 语法规则
  1. 复杂标签: 有子标签
  2. 简单标签: 没有子标签
<!--studnets根标签-->
<xsd: element name = "students" type = "stusType"/>
<xsd: complexType name = "stusType">
<!--complexType: 复杂标签-->
<xsd: sequence>
<xsd: element name = "student" type = "stuType" minOccuers = "0" maxOccuers = "unbounded"/>
</xsd: sequence>
</xsd: complexType>
<!--type: 起别名-->
<!--minOccuers: 最小出现次数-->

<!--student标签-->
<xsd: complexType name = "stuType"/>
<xsd: sequence>
    <xsd: element name = "name" type = "xsd:string"/>
    .
    .
</xsd: sequence>
<xsd: attribute name = "num" type = "numType" use = "required"/>
<!--定义stu属性num-->
<!--required: 必须的-->
</xsd: complexType>

<!--num标签-->
<xsd: simpleType name = "numType">
<!--simpleType: 简单标签-->
<xsd: restriction base = "xsd:string">
<xsd: parrten values = "lk_\d{4}">
<!--"lk_\d{4}: 正则表达式: 以"lk开头加四位整数"-->
</xsd: simpleType> 

image

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值