Schema

schema出现的目的是以一种更加合理的方式来编写XML限制文件,基于xml的方式
schema可以使用命名空间来支持多个名称相同元素
schema可以更好地完成对java或者所有对象的修饰,并且提供了大量的数据类型。

命名空间相当于一个包

<!--
    xmlns:指的就是xml namespace(命名空间),是schema验证的命名空间,
    这是标准的命名空间,我们不能改动,但是可以增加前缀
     
    targetNamespace : 我自己文档的命名空间。方便其他xml或者schema文件的引用。
    要引用的其他的schema的名称的定义地方
     
    xmlns:tns 此处的命名空间域我们自己的命名空间是一致的,单丝增加了tns前缀,如果
    要引用当前文件所创建的类型,需要加上tns前缀
-->

xml中引入命名空间
<!--
xmlns:xsi根据schema产生具体实例的一个命名空间
xsi:schemaLocation 代表schema的名称,我们可以引入我们自己定义的命名空间
或者xsi:noNamespaceSchemaLocation="schema.xsd"也可以引入我们定义的shcema
-->

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/schema_02"
    xmlns:tns="http://www.example.org/schema_02" elementFormDefault="qualified">
    <element name="books">
        <complexType>
            <sequence>
                <element name="book">
                    <complexType>
                        <!-- 表示有序,如果你不想有序可以使用all -->
                        <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>
                                        <sequence>
                                            <element name="author" type="string" />
                                        </sequence>
                                    </complexType>
                                </element>
                            </choice>
                        </sequence>
                        <!-- 定义元素的属性且必须在sequence之后 -->
                        <attribute name="id" type="string" use="required"/>
                    </complexType>
                </element>
            </sequence>
        </complexType>
    </element>
</schema>

猜你喜欢

转载自nicky19870612.iteye.com/blog/2017683