使用schema校验xml
2016-12-02 14:53
267 查看
一、背景
在大型的项目开发中,我们往往要是用xml进行一些规范化的处理。这时候就需要有高手先做好上层的一些规则。这时候往往就会用到schema对xml进行一个规范化的校验
二、代码实践
我们以公司和员工为例来实践一下。
1、创建公司校验schema:CompanySchema.xsd
Xml代码
<?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代码
<?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代码
<?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的命名空间定义成一样的
在大型的项目开发中,我们往往要是用xml进行一些规范化的处理。这时候就需要有高手先做好上层的一些规则。这时候往往就会用到schema对xml进行一个规范化的校验
二、代码实践
我们以公司和员工为例来实践一下。
1、创建公司校验schema:CompanySchema.xsd
Xml代码
<?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代码
<?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代码
<?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的命名空间定义成一样的
相关文章推荐
- JAVA中使用Schema校验XML
- c++中使用xercesc对xml进行schema校验
- 使用DOM4J解析XML及采用Schema校验的方法
- JAVA中使用Schema校验XML
- JAVA中使用Schema校验XML
- XML基础知识——使用schema校验XML文件合法性(四)
- XML中如何使用schema
- 同时使用 xml, schema 和 xslt 的例子
- schema校验文件编写,常用元素使用
- XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式
- 采用Xerces C++执行基于schema的XML文档校验
- XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式
- XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式
- struts2使用XML框架进行输入校验
- XML validation for multiple schemas 验证使用多个XSD schema的XML文件
- Java - 使用 XSD 校验 XML
- Solr的学习使用之(四)建数据库(添加Core)、表(配置schema.xml)
- 对XML文档进行Schema校验的方法(适用于Framework2.0及以上版本)
- Solr的学习使用之(二)schema.xml等配置文件的解析
- MSXML使用技巧:用指定的SCHEMA检验XML