DTD files and XML Schema constraints constraints

XML file Contact.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--  <!DOCTYPE contact SYSTEM "Contact.dtd">-->
<!-- <!DOCTYPE contact SYSTEM "dtd文件的路径"> 
<!DOCTYPE 文档根结点 SYSTEM "DTD文件的URL">
引入公共的DTD:
<!DOCTYPE 文档根结点 PUBLIC "DTD名称" "DTD文件的URL">
文档根结点 指的是当前xml中的根标签。
PUBLIC  表示当前引入的DTD是公共的DTD
-->
<contact xmlns="www.520xlh.com"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="www.520xlh.com Contact.xsd  ">   
	<linkman id="i1">
		<name>张三</name>
		<email>123@163.qq</email>
		<address>陕西省西安市</address>
		<school>西北大学</school>
	</linkman>
	<linkman id="i2">
		<name>李四</name>
		<email>110@163.qq</email>
		<address>陕西西安市</address>
		<school>西安电子科技大学</school>
	</linkman>
</contact>

usage:

<!--  <!DOCTYPE contact SYSTEM "Contact.dtd">-->
<!-- <!DOCTYPE contact SYSTEM "dtd文件的路径"> 
<!DOCTYPE 文档根结点 SYSTEM "DTD文件的URL">
引入公共的DTD:
<!DOCTYPE 文档根结点 PUBLIC "DTD名称" "DTD文件的URL">
文档根结点 指的是当前xml中的根标签。
PUBLIC  表示当前引入的DTD是公共的DTD
-->

Constraints XML DTD file Contact.dtd

<!--新建一个后缀叫dtd的xml文件,写入一下内容-->
<!-- 标签名称 (规范) -->
<!ELEMENT contacts (linkman*)>
<!-- 有序标签 -->
<!ELEMENT linkman (name,email,address,school)>
<!-- 标签名称(需要解析的字符数据) -->
<!ELEMENT name (#PCDATA)>
<!ELEMENT email (#PCDATA)>
<!ELEMENT address (#PCDATA)>
<!ELEMENT school (#PCDATA)>
<!-- 标签名 属性名称 属性类型 默认值  -->
<!--
    ID  表示唯一
    #REQUIRED 表示必填
-->
<!ATTLIST linkman id ID #REQUIRED>

<!--在需要受约束的文件中加入
<!DOCTYPE contacts SYSTEM "dtd的路径">-->

Constraints XML Schema file Contact.xsd

: Constraint specification more than dtd, and a xml file can have multiple schema constraints, and so on.

Precautions:

  1. When defining Schema file for this Schema document itself is xml, it is also subject to other constraints. And this constraint is defined in advance W3C organization,
the Schema file needs to be introduced into the attributes used to come in advance in the root tag:
<Schema xmlns = " http://www.w3.org/2001/XMLSchema introduced" defined W3C written specification schema
targetNamespace = " http://www.itcast.org/book " to the current Schema file a name (namespace)
role which is when xml schema to be introduced this time constraint, the back must be written by current targetNamespace the introduction uri address.

  2.xml how to introduce XSD:
<Element xmlns = " http://www.itcast.org/book " It is the value following schema targetNamespace attribute file of
the xsi: the schemaLocation = " http://www.itcast.org/ Book book.xsd "this is the real path to introduce the current schema file
xmlns: xsi =" http://www.w3.org/2001/XMLSchema-instance "explained the current xml schema is an instance document>

  3.schema namespace:
the definition of Schema file, need to use the root tag
targetNamespace attribute defined in the current schema definition name (just assign a name, did not point to any files), being constrained in the xml file in accordance with the first the introduction of the current schema file name, then in use.
xsi: schemaLocation = "" introduced specific schema file. (Because the schema targetNamespace attribute defines the name, but a name only, so the xml file required to declare the specific location designated followed by Schema file schemaLocation)
(xsi: schemaLocation use when it introduces a schema, first to use the name space, space, file name)
namespace main function is used to
elementFormDefault = "qualified | unqualified"
writing qualified in the schema, you must use the namespace defined in the limited definition of the xml tag name.
unqualified requires root element must use the namespace and the child elements can not use the name space.

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
新建一个后缀叫xsd的xml文件,写入一下内容
    xmlns 当前XML的名称空间 随便写唯一
    xmlns:xs 当前XML中标签来自哪
    targetNamespace 目标文档的名称空间
 -->
<xs:schema xmlns="www.520xlh.com" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="www.520xlh.com" 
    elementFormDefault="qualified">
    <!-- 元素的名称 -->
    <xs:element name="contact">
        <!-- 元素类型复杂类型 -->
        <xs:complexType>
            <!-- 有循序的 -->
            <xs:sequence>
                <!-- 
                    minOccurs 最少出现次数
                    unbounded 没有限制
                -->
                <xs:element name="linkman" minOccurs="0" maxOccurs="unbounded" >
                    <xs:complexType> 
                        <xs:sequence>
                            <!-- type 表示元素的类型 -->
                                <xs:element name="name" type="xs:string"/>
                                <xs:element name="email" type="xs:string"/>
                                <xs:element name="address" type="xs:string"/>
                                <xs:element name="school" type="xs:string"/>
                        </xs:sequence>
                        <!-- user元素上的属性 -->
                        <xs:attribute name="id" type="xs:ID" use="required"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>


<!-- 用法:
    xmlns -> 当前文档的名称空间
    xmlns:xsi -> 当前文档是schema约束的一个实例
    xsi:schemaLocation -> 当前实例支持的约束文件的位置
 -->
<!--<contacts xmlns="www.520it.com"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="www.520it.com xsd的路径">-->

Guess you like

Origin blog.csdn.net/qq_42405666/article/details/90082795