微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

xml之Schema约束

一、什么是Schema约束

Schema,也被称为XML Schema DeFinition(XSD),描述XML文档的结构,是基于XML的DTD替代者,比DTD更强大。

二、Schema的优势

Schema的优势:Schema可针对未来的需求进行扩展,是基于xml编写,支持数据类型,更完善,功能更强大。

通过可扩展的Schema定义,可以:在其他Schema中重复使用您的Schema,创建由标准类型衍生而来的自己的数据类型,在相同的文档中引用多重的Schema。

三、Schema的实际应用:

使用Schema的xml文件

<?xml version="1.0" encoding="UTF-8"?>
<email xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:noNamespaceSchemaLocation="email.xsd">
    <to>liuwei8809@163.com</to>
    <from>hellokity@163.com</from>
    <title>about loving</title>
    <body>I love you</body>
    <date>2008-11-12</date>
</email>

相应的Schema文件

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="email">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="to" type="xs:string"></xs:element>
            <xs:element name="from" type="xs:string"></xs:element>
            <xs:element name="title" type="xs:string"></xs:element>
            <xs:element name="body" type="xs:string"></xs:element>
            <xs:element name="date" type="xs:date"></xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>
</schema>

四、总结:

本文主要总结了什么是Schema,Schema为什么会替代DTD,必然有它自身的优势所在。然后实际给出了一个使用Schema约束的小例子。

原文地址:https://www.jb51.cc/xml/294131.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。