<?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>