XML-Schema

xmlns代表命名空间

XML Schema 使用一套预先规定的XML元素和属性创建的,这些元素和属性定义了XML文档的结构和内容模式。

XML Schema 规定XML文档实例的结构和每个元素/属性的数据类型。

Schema(模式):其作用与DTD一样,也是用于验证XML文档的有效性,只不过它提供比dtd更强大的工程和更细粒度的数据类型,另外,Schema还可以自定义数据类型。

此外Schema也是一个XML,而DTD不是。

所有的Schema必须以Schema作为根元素。

Schema

用法:<xs:schema>//xs不固定,可以改为别的名字

属性:-xmlns,-targetNamespace

元素

作用:声明一个元素

属性:name,type,ref,minOccurs,maxOccurs,substitution,group,fixed,default

test1.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="cat" type="xs:string"/>
<xs:element name="dog" type="xs:string"/>
<xs:complexType name="myType">
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element ref="cat"/>
<xs:element ref="dog">
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="pets" type="myType"/>
</xs:schema>

test1.xml

<?xml version="1.0" encoding="UTF-8"?>
<pets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test1.xsd">
<cat>hello</cat>
<dog>world</dog>
</pets>

group 元素

作用:把一组元素声明组合在一起以便它们能够一起被复合类型应用。

属性:name/ref

attribute 元素

作用:声明一个属性

属性:name/typr/ref/use

attributeGroup元素

作用:把一组属性声明组合在一起,以便可以被复合类型应用

属性:name/ref

simpleType元素

作用:定义一个简单类型,它决定了元素和属性值的约束和相关信息

属性:name

内容:应用已经存在的简单类型,三种方式:

restriction:限定一个范围。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:simpleType name="myType">
<xs:restriction base="xs:integer">
<xs:enumeration value="5"/>

<xs:enumeration value="9"/>
<xs:enumeration value="7"/>
<!--
<xs:minInclusive value="0"/>
<xs:maxInclusive value="100"/>
-->
</xs:restriction>
</xs:simpleType>
<xs:element name="hello" type="myType"></xs:element>

</xs:schema>


list:从列表中选择:从一个特定的数据类型的集合中选择定义一个简单地类型元素。

union:包含一个值的结合:从一个特定简单数据类型的集合中选择定义一个简单类型元素。

test.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:attribute name="allFrameSize">
    <xs:simpleType>
    <xs:union memberTypes="roadbikeSize mountainbikeSize"/>
    </xs:simpleType>
    </xs:attribute>
    
    
    <xs:simpleType name="roadbikeSize">
    <xs:restriction base="xs:positiveInteger">
    <xs:enumeration value="46"/>
<xs:enumeration value="55"/>
<xs:enumeration value="60"/>
</xs:restriction>
</xs:simpleType>

<xs:simpleType name="mountainbikeSize">
<xs:restriction base="xs:string">
<xs:enumeration value="small"/>

<xs:enumeration value="medium"/>
<xs:enumeration value="large"/>    
</xs:restriction>
    </xs:simpleType>
    
<xs:element name="hello">
<xs:complexType>
<xs:sequence>
<xs:element name="welcome" type="xs:string"/>
</xs:sequence>    

<xs:attribute ref="allFrameSize" use="required"/>
</xs:complexType>
    </xs:element>
</xs:schema>
complexType 与simpleType的区别

simpleType类型元素中中不能包含元素或者属性
当需要声明一个元素的子元素或者属性时用complexType

当需要基于内置的基本数据类型,定义一个新的数据类型时,用simpleType

simpleContent元素

作用:应用于complexType,对它的内容进行约束和扩展,表示该complexType没有子元素,同事该ComplexType需要有属性,否则它就成为SimpleType了

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="shoeSize">
    <xs:complexType>
              <xs:simpleContent>             元素下不包含子元素
                  <xs:extension base="xs:decimal">    表示这个元素的类型
                              <xs:attribute name="sizing" use="required">
                                  <xs:simpleType>
                                      <xs:restriction base="xs:string">
                                                <xs:enumeration value="us">
                                                </xs:enumeration>
                                                <xs:enumeration value="europe">
                                                </xs:enumeration>
                                                   <xs:enumeration value="uk">
                                                   </xs:enumeration>
                                          </xs:restriction>
                                          </xs:simpleType>
                                         </xs:attribute>

                               </xs:extension>
                        </xs:simpleContent>
             </xs:complexType>
      </xs:element>    
    </xs:schema>
choice元素

作用:允许唯一的一个元素从一个组中被选择

属性:minOccurs/maxOccurs

sequence元素

作用:给一组元素一个特定的序列

属性:minOccurs/maxOccurs

(按序列输出,与choice不同的是,sequence是以一个序列为单位,而choice一个元素为单位)


总结:

声明元素:

<xs:element>

声明属性:

<xs:attribute>



通过DECTYPR可以明确指定文档的根元素,因为DOCTYPE后面跟的元素就是根元素 ;通过SCHEMA,

无法明确指定目标XML的根元素,XMLSPY是通过推断哪个元素包含了爱他元素来选择包含其他元素最

多的哪个元素作为文档的根,但我们可以明确指定文档的根元素而不必按照XMLSPY的生成来做。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值