XML学习笔记(二)---两种XML约束方式
概述什么是XML约束? 为什么要使用XML约束? XML约束的作用: 常见的XML约束技术: DTD约束引入DTD约束外部引入将dtd的约束内容写在外置的dtd文件中,这个文件后缀必须为.dtd。文件保存时必须用utf-8编码保存。 再在xml文件中使用 <!DOCTYPE 根元素名称 SYSTEM 文件的位置> 来引入这个DTD约束文件。 SYSTEM表明引入的DTD在当前文件系统中,后面指定的文件位置是当前硬盘中的位置。 <!DOCTYPE 文档根结点 PUBLIC "DTD名称" "DTD文件的URL"> PUBLIC表明引入的DTD在网络公共位置中,后面要指明DTD的名字和DTD所在网络位置URL地址 内部引入直接在xml中书写DTD <!DOCTYPE 根元素名称[ dtd约束.... ]> DTD语法元素<!ELEMENT 元素名称 元素约束> 元素约束: #PCDATA 表明包含标签体 + 表示一次或多次 * 0次或多次 ? 0次或一次 也可以使用小括号进行组的操作 属性<!ATTLIST 元素名 属性名 属性类型 属性约束 属性名2 属性类型 属性约束 ...... > 属性类型:
属性约束: #REQUIRED 表明当前属性是一个必须存在的属性,如果这样的属性不存在则在校验时会报错 #IMPLIED 表明当前属性是一个可选的属性,可以有也可以没有 #FIXED "固定值" 表明当前属性具有一个固定值,这样的属性不需要进行赋值,自动就会取这个固定值为值.如果这样的属性指定了一个不是固定值的值则校验报错 "默认值" 表明当前属性具有一个默认值,如果给了其他的值就用其他值,如果没有给值则取这个默认值 ENTITY(实体)<!ENTITY > 就是对一大段内容的引用,可以简化代码的复用 引用实体: <!ENTITY 实体名称 “实体内容” > &实体名称; 参数实体: <!ENTITY % 实体名称 "实体内容"> %实体名称; 例子对于下面这个DTD约束文件 book.dtd <!ELEMENT 书架 (书+)>
<!ELEMENT 书 (书名,作者,售价)>
<!ELEMENT 书名 (#PCDATA)>
<!ELEMENT 作者 (#PCDATA)>
<!ELEMENT 售价 (#PCDATA)>
根据这个约束可以写出如下的xml文件: <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE 书架 SYSTEM "book.dtd">
<书架>
<书>
<书名>Thinking in Java</书名>
<作者>Bruce Eckel</作者>
<售价>79.00元</售价>
</书>
<书>
<书名>第一行代码</书名>
<作者>郭霖</作者>
<售价>68.00元</售价>
</书>
</书架>
Schema约束概述
引入Schema约束如何在xml中引入Schema—名称空间:
<zipstream:书架 xmlns:itcast="http://zipstream.me" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=“http://zipstream.me book.xsd">
Schema的语法语法比较多也比较复杂,具体参照Schema的文档。 例子对于下面这个DTD约束文件 shiporder.xsd <?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="www.itheima.com" elementFormDefault="qualified" >
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
根据这个约束可以写出如下的xml文件: <?xml version="1.0" encoding="utf-8" ?>
<itheima:shiporder xmlns:itheima="www.itheima.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="www.itheima.com shiporder.xsd" orderid="asdf" >
<itheima:orderperson>asdf</itheima:orderperson>
<itheima:shipto>
<itheima:name>zzzz</itheima:name>
<itheima:address>ssss</itheima:address>
<itheima:city>sdfdf</itheima:city>
<itheima:country>sdfdf</itheima:country>
</itheima:shipto>
<itheima:item>
<itheima:title>sadfdsf</itheima:title>
<itheima:note>sdfdsfdsf</itheima:note>
<itheima:quantity>10</itheima:quantity>
<itheima:price>100.99</itheima:price>
</itheima:item>
<itheima:item>
<itheima:title>sadfdsf</itheima:title>
<itheima:note>sdfdsfdsf</itheima:note>
<itheima:quantity>10</itheima:quantity>
<itheima:price>100.99</itheima:price>
</itheima:item>
</itheima:shiporder>
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |