XML Schema


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:group name="myGroup">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="birthday" type="xs:date"/>
<xs:element name="age" type="xs:integer"/>
</xs:sequence>
</xs:group>

<xs:element name="person">
<xs:complexType>
<xs:group ref="myGroup"/>
</xs:complexType>
</xs:element>
</xs:schema>
以上定义了一个组叫myGroup,myGroup中有sequence表示它下面的元素要按照顺序出现。
sequence下面定义了三个元素name birthday age
接着定义了一个element叫person person是一个复合类型 complexType引用了myGroup
那么对应有效的xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Users\BEYOND\Downloads\test1.xsd">
<name>test</name>
<birthday>1988-03-02</birthday>
<age>26</age>
</person>



<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">

<xs:attribute name="interest" type="xs:integer"></xs:attribute>

<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="hello" type="xs:string"/>
<xs:element name="world" type="xs:string"/>
</xs:sequence>
<xs:attribute ref="interest"/>
</xs:complexType>
</xs:element>

</xs:schema>
对应的xml是这样:
在缺省的情况下,属性是可选的。
所以说用XMLSpy根据上面的schema生成xml的时候没有interest属性
<?xml version="1.0" encoding="UTF-8"?>
<person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Users\BEYOND\Downloads\test2.xsd" interest="20">
<hello/>
<world/>
</person>



<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">

<xs:attributeGroup name="myAttributeGroup">
<xs:attribute name="hello" type="xs:string" use="required"/>
<xs:attribute name="world" type="xs:string" use="required"/>
</xs:attributeGroup>

<xs:element name="abcd">
<xs:complexType>
<xs:attributeGroup ref="myAttributeGroup"/>
</xs:complexType>
</xs:element>
</xs:schema>
对应有效的xml:
在缺省的情况下,属性是可选的。
如需规定属性为必选,请使用 "use" 属性 required是必须的意思
<?xml version="1.0" encoding="UTF-8"?>
<abcd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Users\BEYOND\Downloads\test3.xsd" hello="" world=""/>



<?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="7"/>
<xs:enumeration value="9"/>
</xs:restriction>
</xs:simpleType>

<xs:element name="hello" type="myType"/>

</xs:schema>
对应的xml:
<?xml version="1.0" encoding="UTF-8"?>
<hello xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Users\BEYOND\Downloads\test4.xsd">
5
</hello>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值