随笔都是学习笔记
随笔仅供参考,为避免笔记中可能出现的错误误导他人,请勿转载。

简介:

dtd约束格式:

<!ELEMENT 元素名称 约束>

schema符号xml语法;

1、一个xml中只能有一个dtd,但是可以有多个schema;

2、多个schema之间使用名称空间区分:就像java中的包名;

  例如:包1和包2都有一个Element类,但是包1的Element类是节点类型,而包2的Element类是数值类型,当你想要使用数值类型的Element类就需要 包2.Element 导入包2的Element类,这就是用包名进行区分。

3、dtd中有PCDATA类型,但是在schema中可以支持更多的数据类型:

  比如:年龄(整数)-- 在schema中可以直接定义整数类型;

4、schema语法更加复杂,目前不能取代dtd;

 

快速入门:

1、创建schema文件(.xsd)

根节点:<Schema>

schema文档也是xml,那么怎么表示schema是约束xml的文档:

xmlns="http://www.w3.org/2001/XMLSchema"

上面的属性值就表示本schema文档是约束文档;

targetNamespace="http://www.example.org/schema1_1" 

上面的属性值表示引入schema文档的使用路径;

elementFormDefault="qualified"

文档默认值:qualified表示当引入schema文档时进行了命名,那么在xml中使用此文档的元素标签时就都必须以<命名:元素标签>的形式使用

例如在引入company.xsd是进行了命名为 xmlns:company 那么在本xml中使用company的<name>标签就必须以<company:name>的形式进行创建

创建一个person.xml:

<?xml version="1.0" encoding="UTF-8"?>
<person>
    <name>zs</name>
    <age>12</age>
</person>

根据person.xml创建schema.xsd:    

  (1、)看xml文档中有多少个元素

    有几个元素就写几个<element>

  (2、)看简单元素个复杂元素

    如果是复杂元素:

<element name="复杂元素名称">
        <complexType>
            <sequence>
          子元素
</sequence> </complexType> </element>

     如果是简单元素:写在复杂元素里面

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.org/schema1_1"
    xmlns:tns="http://www.example.org/schema1_1"
    elementFormDefault="qualified">
    <element name="person">
        <complexType>
            <sequence>
                <element name="name" type="string"></element>
                <element name="age" type="int"></element>
            </sequence>
        </complexType>
    </element>
</schema>
<targetNamespace>:路径名称,可以自定义,一般为默认;
<sequence>:按顺序定义

 

(4、)在被约束的文档(xml)中引入约束文档(schema):

 

<?xml version="1.0" encoding="UTF-8"?>
<person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.example.org/schema1_1"
xsi:schemaLocation="http://www.example.org/schema1_1 schema1_1.xsd">
    <name>zs</name>
    <age>12</age>
</person>

 

在根元素加上属性值:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"表示被约束的xml文档(注意-instance),其中 : 后的 xsi 就是命名空间(类似于包名,可以自定义);

xmlns="http://www.example.org/schema1_1" 表示引入文档的路径

按照《命名空间:schemaLocation="targetNameSpace name.xsd"》的形式来定义: xsi:schemaLocation="http://www.example.org/schema1_1 schema1_1.xsd"注意空格

 

当引入schema约束文档之后再创建新的标签:

 

会出现错误;

注意:如果引入多个schema文档,那么就可以添加多个 xmlns 属性,然后用命名空间来进行区分;

例如有company.xsd 和 dept.xsd 两个schema文档,

那么就可以写入:

 

xmlns:company="company文档路径"
xmlns:dept="dept文档路径"

xmlns:xsi="....-instance"
xsi:schemaLocation="company文档路径 company.xsd dept文档路径 dept.xsd"

 

 

上面定义命名空间和文档路径,下面进行文档引入;

如果在xml中定义的标签在company和dept中同时存在,那么可以使用<命名空间:标签名>的方式定义

例如company.xsd 和 dept.xsd 中同时存在<name>这个标签,当你想要使用company.xsd中的<name>标签时,就可以使用<company:name>进行定义,表示使用的是company.xsd中的<name>标签。

 

 

元素标签定义:

定义属性:必须定义在复杂元素,写在</complexType>前面

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.org/schema1_1"
    xmlns:tns="http://www.example.org/schema1_1"
    elementFormDefault="qualified">

    <element name="person">
        <complexType>
            <sequence>
                <element name="name" type="string"></element>
                <element name="age" type="int"></element>
            </sequence>
            <attribute name="id1" type="int" use="required"></attribute>
        </complexType>
    </element>
</schema>

name:属性的名称

type:属性的类型

use:表示属性是否必须出现(required:必须出现)

那么对应的xml文档的那个复杂元素中就必须出现"id1"这个属性值:

<?xml version="1.0" encoding="UTF-8"?>
<person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.example.org/schema1_1"
xsi:schemaLocation="http://www.example.org/schema1_1 schema1_1.xsd" 
id1="12">
    <name>zs</name>
    <age>12</age>
</person>

 

posted on 2022-03-25 17:16  时间完全不够用啊  阅读(462)  评论(0编辑  收藏  举报