schema几种编写方式

原文转载自 http://blog.163.com/qazwsxzhx@126/blog/static/1844315420123710224597/

(1)只有一个根元素的方式

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/test" xmlns:tns="http://www.example.org/test" elementFormDefault="qualified">
<element name="user">
<complexType>
<sequence>// 可以把sequence改成all,表示id,username,password,birhdate这些可以在xml中无序写。
<element name="id" type="int"></element>
<element name="username" type="string"></element>
<element name="password" type="string"></element>
<element name="birthdate" type="date"></element>
</sequence>
</complexType>
</element>
</schema>

结构清晰,但是元素不能重用。

(2)多个根元素

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/NewXMLSchema1"
xmlns:tns="http://www.example.org/NewXMLSchema1" elementFormDefault="qualified">
<element name="users" type="tns:usertype"></element>
<element name="id" type="int"></element>
<element name="username" type="string"></element>
<element name="password" type="string"></element>
<complexType name="usertype">
<sequence minOccurs="1" maxOccurs="unbounded">
<element name="user">
<complexType>
<sequence>
<element ref="tns:id"></element>
<element ref="tns:username"></element>
<element ref="tns:username"></element>
</sequence>
</complexType>
</element>
</sequence>
</complexType>
</schema>

多个根节点,但是不清楚哪个是根节点。达到了element代码重用。

 

(3)上面两种方式的结合。

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/NewXMLSchema1"
xmlns:tns="http://www.example.org/NewXMLSchema1" elementFormDefault="qualified">
<element name="users" type="tns:usertype"></element>
<complexType name="usertype">
<sequence minOccurs="1" maxOccurs="unbounded">
<element name="user">
<complexType>
<sequence>
<element name="id" type="tns:idType"></element>
<element name="username" type="string"></element>
<element name="password" type="string"></element>
<element name="email" type="tns:emailType"></element>
</sequence>
<attribute name="sex" type="tns:sexType" ></attribute>
</complexType>
</element>
</sequence>
</complexType>

<!--限定元素类型-->
<simpleType name="idType">
<restriction base="int">
<minInclusive value="1"></minInclusive>
</restriction>
</simpleType>
<simpleType name="emailType">
<restriction base="string">
<pattern value="(\w+\.*)*\w+@\w+\.[A-Za-z]{2,6}"></pattern>
</restriction>
</simpleType>
<simpleType name="sexType">
<restriction base="string">
<enumeration value="男"></enumeration>
<enumeration value="女"></enumeration>
</restriction>
</simpleType>
</schema>

 

(4)包含

student.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/NewXMLSchema4"
xmlns:tns="http://www.example.org/NewXMLSchema4" elementFormDefault="qualified">
<element name="student" type="tns:studenttype"></element>
<complexType name="studenttype">
<sequence>
<element name="id" type="tns:idType"></element>
<element name="username" type="string"></element>
<element name="password" type="string"></element>
<element name="email" type="tns:emailType"></element>
</sequence>
<attribute name="sex" type="tns:sexType" ></attribute>
</complexType>
</element>
</sequence>
</complexType>
<simpleType name="idType">
<restriction base="int">
<minInclusive value="1"></minInclusive>
</restriction>
</simpleType>
<simpleType name="emailType">
<restriction base="string">
<pattern value="(\w+\.*)*\w+@\w+\.[A-Za-z]{2,6}"></pattern>
</restriction>
</simpleType>
<simpleType name="sexType">
<restriction base="string">
<enumeration value="男"></enumeration>
<enumeration value="女"></enumeration>
</restriction>
</simpleType>
</schema>

class.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/NewXMLSchema2"
xmlns:tns="http://www.example.org/NewXMLSchema2" elementFormDefault="qualified">
<include schemaLocation="student.xsd"></include>
<element name="classroom" type="tns:classroom"></element>
<complexType name="classroom">
<sequence>
<element name="grade" type="tns:gradeType"></element>
<element name="student" type="tns:studenttype"></element>
</sequence>
</complexType>
<simpleType name="gradeType">
<restriction base="int">
<minInclusive value="2001"></minInclusive>
<maxExclusive value="3000"></maxExclusive>
</restriction>
</simpleType>
</schema> 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值