<?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>
1.http://www.w3.org/2001/XMLSchema
Schema 默认命名空间,不能修改,但是可以增加前缀。如何增加前缀之后所有的element元素都要增加浅醉
2.targetNamespace="http://www.example.org/01"
自己这个文档的命令空间,可以方便其他xml和schema文件的引用
3.xmlns:tns="http://www.example.org/01"
此处的名称和自己的命名空间一直,增加了tns的前缀,此时如果要引用当前文件创建的类型,需要加上tns前缀
<?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 01.xsd">
<id>1</id>
<username>wxh</username>
<born>1998-12-12</born>
</user>
4.复杂的schema文件和xml文件相关配置
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/04"
xmlns:tns="http://www.example.org/04"
elementFormDefault="qualified">
<element name="person" type="tns:personType"/>
<complexType name="personType">
<sequence>
<element name="name" type="string"/>
<element name="age" type="tns:ageType"/>
<element name="email" type="tns:emailType"/>
</sequence>
<attribute name="sex" type="tns:sexType"/>
</complexType>
<simpleType name="emailType">
<restriction base="string">
<pattern value="(\w+\.*)*\w+@\w+\.[A-Za-z]{2,6}"/>
<minLength value="6"/>
<maxLength value="255"/>
</restriction>
</simpleType>
<simpleType name="ageType">
<restriction base="int">
<minInclusive value="1"/>
<maxExclusive value="150"/>
</restriction>
</simpleType>
<simpleType name="sexType">
<restriction base="string">
<enumeration value="男"/>
<enumeration value="女"/>
</restriction>
</simpleType>
</schema>
<?xml version="1.0" encoding="UTF-8"?>
<person xmlns="http://www.example.org/04"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/04 04.xsd" sex="男">
<name>搜索</name>
<age>149</age>
<email>[email protected]</email>
</person>
54.复杂的schema文件
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/classroom"
xmlns:tns="http://www.example.org/classroom"
elementFormDefault="qualified">
<xsd:element name="student" type="tns:studentType"/>
<xsd:complexType name="studentType">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="sex" type="tns:sexType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="sexType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="男"/>
<xsd:enumeration value="女"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/classroom"
xmlns:tns="http://www.example.org/classroom"
elementFormDefault="qualified">
<xsd:include schemaLocation="student.xsd"/>
<xsd:element name="classroom" type="tns:classroomType"/>
<xsd:complexType name="classroomType">
<xsd:sequence>
<xsd:element name="grade" type="tns:gradeType"/>
<xsd:element name="name" type="xsd:string"/>
<!-- <xsd:element name="stus">
<xsd:complexType>
<xsd:sequence minOccurs="1" maxOccurs="unbounded">
<xsd:element name="student" type="tns:studentType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
-->
<xsd:sequence minOccurs="1" maxOccurs="unbounded">
<xsd:element name="student" type="tns:studentType"/>
</xsd:sequence>
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="gradeType">
<xsd:restriction base="xsd:int">
<xsd:minInclusive value="2000"/>
<xsd:maxInclusive value="3000"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>