一、背景
在大型的项目开发中,我们往往要是用xml进行一些规范化的处理。这时候就需要有高手先做好上层的一些规则。这时候往往就会用到schema对xml进行一个规范化的校验
二、代码实践
我们以公司和员工为例来实践一下。
1、创建公司校验schema:CompanySchema.xsd
2、创建员工校验schema:EmpSchema.xsd
3、创建公司xml:Company.xml
4、注意点
要将EmpSchema.xsd,CompanySchema.xsd的命名空间定义成一样的
在大型的项目开发中,我们往往要是用xml进行一些规范化的处理。这时候就需要有高手先做好上层的一些规则。这时候往往就会用到schema对xml进行一个规范化的校验
二、代码实践
我们以公司和员工为例来实践一下。
1、创建公司校验schema:CompanySchema.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/company"
xmlns:tns="http://www.example.org/company">
<xsd:element name="emp" type="tns:empType"/>
<xsd:complexType name="empType">
<xsd:sequence>
<xsd:element name="empName" type="xsd:string"/>
<xsd:element name="empId" type="tns:empIdType"/>
<xsd:element name="sex" type="tns:sexType"/>
<xsd:element name="age" type="tns:ageType" />
<xsd:element name="email" type="tns:emailType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="empIdType">
<xsd:restriction base="xsd:int">
<xsd:minInclusive value="1" />
<xsd:maxExclusive value="100000" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="sexType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="男" />
<xsd:enumeration value="女" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ageType">
<xsd:restriction base="xsd:int">
<xsd:minInclusive value="1" />
<xsd:maxExclusive value="120" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="emailType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="(\w+\.*)*\w+@\w+\.[A-Za-z]{2,6}" />
<xsd:minLength value="6" />
<xsd:maxLength value="255" />
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
2、创建员工校验schema:EmpSchema.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/company"
xmlns:tns="http://www.example.org/company">
<xsd:element name="emp" type="tns:empType"/>
<xsd:complexType name="empType">
<xsd:sequence>
<xsd:element name="empName" type="xsd:string"/>
<xsd:element name="empId" type="tns:empIdType"/>
<xsd:element name="sex" type="tns:sexType"/>
<xsd:element name="age" type="tns:ageType" />
<xsd:element name="email" type="tns:emailType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="empIdType">
<xsd:restriction base="xsd:int">
<xsd:minInclusive value="1" />
<xsd:maxExclusive value="100000" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="sexType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="男" />
<xsd:enumeration value="女" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ageType">
<xsd:restriction base="xsd:int">
<xsd:minInclusive value="1" />
<xsd:maxExclusive value="120" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="emailType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="(\w+\.*)*\w+@\w+\.[A-Za-z]{2,6}" />
<xsd:minLength value="6" />
<xsd:maxLength value="255" />
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
3、创建公司xml:Company.xml
<?xml version="1.0" encoding="UTF-8"?>
<Company xmlns="http://www.example.org/company" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/company">
<companyName>中国实业集团</companyName>
<comppanyAddress>北京市朝阳区呼家楼</comppanyAddress>
<companyEmail>gxsenjoy@163.com</companyEmail>
<emp>
<empName>王刚</empName>
<empId>12345</empId>
<sex>男</sex>
<age>100</age>
<email>wanggang@sino.com</email>
</emp>
<emp>
<empName>瓜瓜</empName>
<empId>100</empId>
<sex>男</sex>
<age>100</age>
<email>guagua@sino.com</email>
</emp>
</Company>
4、注意点
要将EmpSchema.xsd,CompanySchema.xsd的命名空间定义成一样的