XML--XML Schema Definition(三)

参考

http://www.w3school.com.cn/schema/index.asp

 

XSD 复合元素

复合元素指包含其他元素及/或属性的 XML 元素。

有四种类型的复合元素:

  • 空元素
  • 包含其他元素的元素
  • 仅包含文本的元素
  • 包含元素和文本的元素

注释:上述元素均可包含属性!

 

定义复合元素(xs:complexType):

1.通过命名此元素,直接对元素进行声明

1 <xs:element name="employee">
2   <xs:complexType>
3     <xs:sequence>
4       <xs:element name="firstname" type="xs:string"/>
5       <xs:element name="lastname" type="xs:string"/>
6     </xs:sequence>
7   </xs:complexType>
8 </xs:element>

2.使用 type 属性,这个属性的作用是引用要使用的复合类型的名称

1 <xs:element name="employee" type="personinfo"/>
2 
3 <xs:complexType name="personinfo">
4   <xs:sequence>
5     <xs:element name="firstname" type="xs:string"/>
6     <xs:element name="lastname" type="xs:string"/>
7   </xs:sequence>
8 </xs:complexType>

 

复合空元素:

空的复合元素不能包含内容,只能含有属性。

1 <product prodid="1345" />
复制代码
1 <xs:element name="product">
2   <xs:complexType>
3     <xs:complexContent>
4       <xs:restriction base="xs:integer">
5         <xs:attribute name="prodid" type="xs:positiveInteger"/>
6       </xs:restriction>
7     </xs:complexContent>
8   </xs:complexType>
9 </xs:element>
复制代码

1 <xs:element name="product">
2   <xs:complexType>
3     <xs:attribute name="prodid" type="xs:positiveInteger"/>
4   </xs:complexType>
5 </xs:element>

或:

1 <xs:element name="product" type="prodtype"/>
2 
3 <xs:complexType name="prodtype">
4   <xs:attribute name="prodid" type="xs:positiveInteger"/>
5 </xs:complexType>

 

复合类型仅包含元素:

“仅含元素”的复合类型元素是只能包含其他元素的元素。

1 <person>
2     <firstname>John</firstname>
3     <lastname>Smith</lastname>
4 </person>
1 <xs:element name="person">
2   <xs:complexType>
3     <xs:sequence>
4       <xs:element name="firstname" type="xs:string"/>
5       <xs:element name="lastname" type="xs:string"/>
6     </xs:sequence>
7   </xs:complexType>
8 </xs:element>

1 <xs:element name="person" type="persontype"/>
2 
3 <xs:complexType name="persontype">
4   <xs:sequence>
5     <xs:element name="firstname" type="xs:string"/>
6     <xs:element name="lastname" type="xs:string"/>
7   </xs:sequence>
8 </xs:complexType>

 

仅含文本的复合元素:

仅含文本的复合元素可包含文本和属性。

此类型仅包含简易的内容(文本和属性),因此我们要向此内容添加 simpleContent 元素。当使用简易内容时,我们就必须在 simpleContent 元素内定义扩展或限定.

1 <shoesize country="france">35</shoesize>
复制代码
1 <xs:element name="shoesize">
2   <xs:complexType>
3     <xs:simpleContent>
4       <xs:extension base="xs:integer">
5         <xs:attribute name="country" type="xs:string" />
6       </xs:extension>
7     </xs:simpleContent>
8   </xs:complexType>
9 </xs:element>
复制代码

复制代码
1 <xs:element name="shoesize" type="shoetype"/>
2 
3 <xs:complexType name="shoetype">
4   <xs:simpleContent>
5     <xs:extension base="xs:integer">
6       <xs:attribute name="country" type="xs:string" />
7     </xs:extension>
8   </xs:simpleContent>
9 </xs:complexType>
复制代码

 

带有混合内容的复合类型:

混合的复合类型可包含属性、元素以及文本。

1 <letter>
2 Dear Mr.<name>John Smith</name>.
3 Your order <orderid>1032</orderid>
4 will be shipped on <shipdate>2001-07-13</shipdate>.
5 </letter>
复制代码
1 <xs:element name="letter">
2   <xs:complexType mixed="true">
3     <xs:sequence>
4       <xs:element name="name" type="xs:string"/>
5       <xs:element name="orderid" type="xs:positiveInteger"/>
6       <xs:element name="shipdate" type="xs:date"/>
7     </xs:sequence>
8   </xs:complexType>
9 </xs:element>
复制代码

复制代码
1 <xs:element name="letter" type="lettertype"/>
2 
3 <xs:complexType name="lettertype" mixed="true">
4   <xs:sequence>
5     <xs:element name="name" type="xs:string"/>
6     <xs:element name="orderid" type="xs:positiveInteger"/>
7     <xs:element name="shipdate" type="xs:date"/>
8   </xs:sequence>
9 </xs:complexType>
复制代码

 

作者:MicroCat
画图工具
仅作记录日常工作学习的点滴,如发现错误,望请纠正。

posted @   MicroCat  阅读(216)  评论(0编辑  收藏  举报
编辑推荐:
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· 一个基于 .NET 开源免费的异地组网和内网穿透工具
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
点击右上角即可分享
微信分享提示