惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
阮一峰的网络日志
阮一峰的网络日志
云风的 BLOG
云风的 BLOG
D
Docker
Vercel News
Vercel News
IT之家
IT之家
Recent Announcements
Recent Announcements
Last Week in AI
Last Week in AI
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
腾讯CDC
Google DeepMind News
Google DeepMind News
I
InfoQ
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
The GitHub Blog
The GitHub Blog
博客园 - Franky
The Cloudflare Blog
A
About on SuperTechFans
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
T
Tenable Blog
P
Proofpoint News Feed
Recorded Future
Recorded Future
Security Latest
Security Latest
H
Hackread – Cybersecurity News, Data Breaches, AI and More
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 聂微东
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Google Online Security Blog
Google Online Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Simon Willison's Weblog
Simon Willison's Weblog
The Last Watchdog
The Last Watchdog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
U
Unit 42
The Hacker News
The Hacker News
Martin Fowler
Martin Fowler
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
F
Full Disclosure
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
V
V2EX
Project Zero
Project Zero

博客园 - JackMa

设计模式 外观 Facade 设计模式 适配器-Adapter 设计模式 命令-Command 设计模式 单件-Singleton 积累PDU 设计模式 工厂-Factory 设计模式 策略-Strategy,装饰-Decorator,观察者-Observer Java多线程编程 Red Hat Linux认证 认识Agile,Scrum和DevOps 博客目标 iOS Development Learning 13Nov 重新起步 iOS 开发 XML学习笔记(七)Schema语法杂项 UML和模式应用-第一部分:绪论 XML学习笔记(五)Schema语法之简单类型 XML学习笔记(四)Schema介绍篇 XML学习笔记(三)进阶篇 Xml学习笔记(二)Javascript篇
XML学习笔记(六)Schema语法之复杂类型
JackMa · 2008-05-31 · via 博客园 - JackMa

Preface:本文是W3Schools上《Schema指南》的学习笔记。其中大部分内容是对指南的翻译总结。由于原文的例子更详尽生动,如果各位想阅读原文可以到这个网址http://www.w3schools.com/schema/default.asp。同时,W3Schools提供了测试,大家可以测试一下自己的理解程度。

        所谓的复杂类型Complex Type其实就是指ComplexElement。概括的说,包含有属性或其他元素的Element,就称为ComplexElement。ComplexElement可分为四种:

  • empty elements
    <product pid="1345"/>
    自闭合的Element,同时由于包含有Attribute的关系所以属于ComplexElement。

  • 只包含其他Elements的元素
    <employee>
    <firstname>John</firstname>
    <lastname>Smith</lastname>
    </employee>

  • 只包含Text的Element
    <food type="dessert">Ice cream</food>
    说明这里与SimpleElement不同的是,“type”是自定义的数据类型而且是一个ComplexType,而不是Schema内置的。同时这个Element是带有属性的。

  • 同时包含有其他Element和Text的元素
    <description>
    It happened on <date lang="norwegian">03.03.99</date> ....
    </description>

一、概览ComplexElement

定义一个ComplexElement
例如在XML中有如下元素

<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>

使用Schema定义就是

<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

注意这里的ComplexType不能被其他元素使用,所以如果要考虑重用,可以使用下面的方式。

<xs:element name="employee" type="personinfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType> 

这里的格式与之前定义约束的时候很相似。不过约束是使用<xs:simpleType>定义的。

重用已定义的ComplexElement
说到重用,你还可以在现有的ComplexType基础上进行扩展,形成新的ComplexType。如下:

<xs:element name="employee" type="fullpersoninfo"/><xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType><xs:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:extension base="personinfo">
<xs:sequence>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>

注意关键的是用“base”属性指出是对哪个元素进行扩展。这里fullpersioninfo将包含firstname,lastname,address,city,country的信息。

二、Empty Element

有一个EmptyElement如下

<product prodid="1345" /> 

那么使用定义应为

<xs:element name="product">
<xs:complexType>
<xs:attribute name="prodid" type="xs:positiveInteger"/>
</xs:complexType>
</xs:element> 

注意,你仍然可以使用“name”属性为complexType指定名称,然后在element中使用“type”属性指定类型。从而能够在多个element中使用这个类型定义。

三、Elements Only

以下的ComplexType是只包含其他元素的。

<person>
<firstname>John</firstname>
<lastname>Smith</lastname>
</person> 

在Schema中定义如下

<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

这里要注意的是<xs:sequence>的意义,是指里面包含的元素(firstname,lastname)在XML中(person)必须按照定义的顺序出现。

四、Complex Text-Only Elements

<shoesize country="france">35</shoesize>
定义如下

<xs:element name="shoesize">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="country" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element> 

这里要注意的是由于Element包含简单的内容和属性,所以要使用<xs:simpleContent>将其括住。同时,因为使用了<xs:simpleContent>就必须定义一个扩展(extension)或约束(restriction)。

五、同时包含有其他Element和Text的元素(mix属性)

<letter>
Dear Mr.
<name>John Smith</name>.
Your order 
<orderid>1032</orderid>
will be shipped on 
<shipdate>2001-07-13</shipdate>.
</letter><xs:element name="letter">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="orderid" type="xs:positiveInteger"/>
<xs:element name="shipdate" type="xs:date"/>
</xs:sequence>
</xs:complexType>
</xs:element>

注意,由于有字符出现在元素之间,所以要设定complexType的mixed属性为true。

本文为个人原创,转载请注明出自:http://jackma.cnblogs.com/
Author:JackMa