xml从入门到精通之schema文件

本文介绍了XML Schema的基本概念及其如何用于约束XML文档。通过实例详细展示了如何使用Schema定义复杂的XML结构,包括基本类型如整数、字符串及日期等,并进一步探讨了更高级的主题,例如元素的重复次数控制和选择性内容组织。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

dtd文件的作用就是约束xml文档,但是他不够牛逼,不够灵活。所以schema就孕育而生!!!

01.xsd //sechma文件的后缀是xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
        targetNamespace="http://www.example.org/01"
        xmlns:tns="http://www.example.org/01" 
        elementFormDefault="qualified">
    <element name="user">
        <complexType>
            <sequence>
                <element name="id" type="int"/>
                <element name="username" type="string"/>
                <element name="born" type="date"/>
            </sequence>
        </complexType>
    </element>
</schema>

这里写图片描述

<?xml version="1.0" encoding="UTF-8"?>
<user xmlns="http://www.example.org/01"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.example.org/01">
    <id>1</id>
    <username>zhangsan</username>
    <born>1989-12-22</born>
</user>

写好之后引入xml文件
这里写图片描述
另外一种引入方式
这里写图片描述

下面写几个比较常用的几个参数

02.xsd

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

    <element name="books">
        <complexType>
        <!-- maxOccurs表示最大出现次数 -->
            <sequence maxOccurs="unbounded">
                <element name="book">
                    <complexType>
                        <sequence minOccurs="1" maxOccurs="unbounded">
                            <element name="title" type="string" />
                            <element name="content" type="string" />
                            <choice>
                                <element name="author" type="string" />
                                <element name="authors">
                                    <complexType>
                                        <all><!-- 每个元素只能出现一次 -->
                                            <element name="author" type="string"/>
                                        </all>
                                    </complexType>
                                </element>
                            </choice>
                        </sequence>
                        <attribute name="id" type="int" use="required"/>
                    </complexType>
                </element>
            </sequence>
        </complexType>
    </element>

</schema>

这里写图片描述
02.xml

<?xml version="1.0" encoding="UTF-8"?>
<book:books xmlns:book="http://www.example.org/02"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="02.xsd">
    <book:book id="1">
        <book:title>Java in action</book:title>
        <book:content>Java is good</book:content>
        <book:author>Bruce</book:author>
    </book:book>
    <book:book id="2">
        <book:title>SOA in action</book:title>
        <book:content>soa is difficult</book:content>
        <book:authors>
            <book:author>Jike</book:author>
        </book:authors>
    </book:book>
</book:books>

这里定义book的属性,一定要写在sequence之后
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

发疯的man

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值