新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> DTD, XML Schema(XMLS), RELAX NG
    [返回] 中文XML论坛 - 专业的XML技术讨论区XML.ORG.CN讨论区 - XML技术『 DTD/XML Schema 』 → [转帖]XML Schema教程 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 49064 个阅读者浏览上一篇主题  刷新本主题   平板显示贴子 浏览下一篇主题
     * 贴子主题: [转帖]XML Schema教程 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     Qr 帅哥哟,离线,有人找我吗?
      
      
      威望:9
      等级:博士二年级(版主)
      文章:4392
      积分:29981
      门派:XML.ORG.CN
      注册:2004/5/15

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给Qr发送一个短消息 把Qr加入好友 查看Qr的个人资料 搜索Qr在『 DTD/XML Schema 』的所有贴子 访问Qr的主页 引用回复这个贴子 回复这个贴子 查看Qr的博客楼主
    发贴心情 [转帖]XML Schema教程

    1. 最简单的Schema文档
    如何写一个最简单的XML Schema文档呢?

    首先,我们写出一个最简单的XML文档。

    hello.xml

    -------------------

    <?xml version="1.0"?>

    <greeting>Hello World!!</greeting>

    <!--一个根元素:greeting;且这个元素不含属性,无子元素,内容是字符串。-->

    hello.xsd

    ----------

    <?xml version="1.0"?>

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <xsd:element name="greeting" type="xsd:string"/>
    </xsd:schema>

    XML Schema文档后缀名是.xsd,完全符合XML语法,根元素是schema,命名空间xmlns:xsd="http://www.w3.org/2001/XMLSchema,用元素<element>定义实例文档中的元素,如greeting。

    2. 含子元素的Schema文档
    假设实例文档是如下的:

    customer.xml

    -----------

    <customer>

    <name>teiki</name>

    <address>No.237, Road Waitan, Shanghai</address>

    </customer>

    则可以写出以下的XML Schema文档:

    customer.xsd

    ----------------

    <?xml version="1.0"?>

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <xsd:element name="customer">

    <xsd:complexType>

    <xsd:sequence>

    <xsd:element name="name" type="xsd:string"/>

    <xsd:element name="address" type="xsd:string" />

    </xsd:sequence>

    </xsd:complexType>

    </xsd:element>

    </xsd:schema>

    实例文档customer.xml中,<customer>元素含有两个子元素,所以我们在Schema文档中采用ComplexType来定义该元素。sequence表示子元素依次出现的顺序。

    3. 含子元素和孙元素的Schema文档
    这次我们给出一个更加复杂一些的文档:

    customer.xml

    ---------------

    <customer>

    <name>Teiki</name>

    <address>

    <!-- address追加一个地址子元素 -->

    <prefecture>Zhejiang</prefecture>

    <city>Hangzhou</city>

    <street>Xilu Road, No.121, 7F</street>

    </address>

    </customer>

    为此,我们需要一个更加复杂一点的Schema文档:

    address.xsd

    -----------------

    <?xml version="1.0"?>

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <xsd:element name="customer">

    <xsd:complexType>

    <xsd:sequence>

    <xsd:element name="name" type="xsd:string"/>

    <!-- 追加子元素address-->

    <xsd:element name="address">

    <xsd:complexType>

    <xsd:sequence>

    <xsd:element name="prefecture" type="xsd:string"/>

    <xsd:element name="city" type="xsd:string" />

    <xsd:element name="street" type="xsd:string" />

    </xsd:sequence>

    </xsd:complexType>

    </xsd:element>

    </xsd:sequence>

    </xsd:complexType>

    </xsd:element>

    </xsd:schema>

    不过,我们还可以采用ref元素来重新编写这个Schema文档:

    address2.xsd

    ----------------------

    <?xml version="1.0"?>

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <xsd:element name="customer">

    <xsd:complexType>

    <xsd:sequence>

    <xsd:element name="name" type="xsd:string"/>

    <xsd:element ref="address"/>

    </xsd:sequence>

    </xsd:complexType>

    </xsd:element>

    <xsd:element name="address">

    <xsd:complexType>

    <xsd:sequence>

    <xsd:element name="prefecture" type="xsd:string"/>

    <xsd:element name="city" type="xsd:string" />

    <xsd:element name="street" type="xsd:string" />

    </xsd:sequence>

    </xsd:complexType>

    </xsd:element>

    </xsd:schema>

    使用ref元素可以直接将其指向另一个模块,使文档更加具有可读性。

    4. 定义相同子元素的数量
    先看这个简单的订购数据实例文档:

    order.xml

    ---------

    <order>

    <orderItem>Accounting Book</orderItem>

    <orderItem>Taxation Book</orderItem>

    </order>

    假设<orderItem>元素,即每次的订购书目不能超过10种,那该怎么写这个Schema文档呢?这里要用到<element>的maxOccurs属性。

    order.xsd

    --------------------

    <?xml version="1.0"?>

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <xsd:element name="order">

    <xsd:complexType>

    <xsd:sequence>

    <xsd:element name="orderItem" type="xsd:string" maxOccurs="10" />

    </xsd:sequence>

    </xsd:complexType>

    </xsd:element>

    </xsd:schema>

    第7行中的maxOccurs属性为10,代表orderItem元素可以最大有10个。如果,不设定元素个数,则可以用maxOccurs="unbounded"来定义。

    类似,如果要定义最小值,可以使用minOccurs,比如下面这句:

    <xsd:element name="orderItem" type="xsd:string" minOccurs="5" maxOccurs="10"/>

    这两个属性缺省值都是1。

    5. 定义可选项的子元素
    假如上面的订书数据中,可以用书名或者书号任一一种订购,则实例文档可能如下:

    order2.xml

    -----------------

    <order>

    <orderItem>

    <!--书名订购-->

    <name>Accounting Book</name>

    </orderItem>

    <orderItem>

    <!--书号订购-->

    <id>7-5058-3496-7</id>

    </orderItem>

    </order>

    这时书写Schema文档还需要使用choice元素。

    order2.xsd

    -------------------------

    <?xml version="1.0"?>

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <xsd:element name="order">

    <xsd:complexType>

    <xsd:sequence>

    <xsd:element ref="orderItem" maxOccurs="10" />

    </xsd:sequence>

    </xsd:complexType>

    </xsd:element>

    <xsd:element name="orderItem">

    <xsd:complexType>

    <xsd:choice>

    <xsd:element name="name" type="xsd:string"/>

    <xsd:element name="id" type="xsd:string"/>

    </xsd:choice>

    </xsd:complexType>

    </xsd:element>

    </xsd:schema>

    稍微更复杂的可选项子元素

    再稍微修改一下订书数据的实例文档:

    order3.xml

    -----------------

    <order>

    <orderItem>

    <name>Accounting Book</name>

    <quantity>2</quantity>

    </orderItem>

    <orderItem>

    <id>7-5058-3496-7</id>

    </orderItem>

    </order>

    这里假定<quantity>值为1时,缺省。

    如何修改Schema文档呢?

    order3.xsd

    -----------------

    <?xml version="1.0"?>

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <xsd:element name="order">

    <xsd:complexType>

    <xsd:sequence>

    <xsd:element ref="orderItem" maxOccurs="10"/>

    </xsd:sequence>

    </xsd:complexType>

    </xsd:element>

    <xsd:element name="orderItem">

    <xsd:complexType>

    <xsd:sequence>

    <xsd:choice>

    <xsd:element name="name" type="xsd:string"/>

    <xsd:element name="id" type="xsd:string"/>

    </xsd:choice>

    <xsd:element name="quantity" type="xsd:string" minOccurs="0"/>

    </xsd:sequence>

    </xsd:complexType>

    </xsd:element>

    </xsd:schema>

    19行中的quantity最少出现值为0,也就是可以有,也可以没有。
    当然,也可以直接在<choice>元素中,包含quantity,然后定义它的minOccurs。

    6. 内置简单类型
    图省略

    7. 自定义简单类型
    如果内置简单类型的44种还不能满足要求,怎么办呢?下面学习自定义简单类型。(XML的扩展性充分体现在这里)

    例如这个实例文档:

    order4.xml

    -----------------

    <order>

    <orderItem>

    <id>7-5058-3496-7</id>

    <quantity>5</quantity>

    </orderItem>

    </order>

    ID是一个标准的ISBN编码,我们怎么定义这个ISBN编码呢?

    <xsd:simpleType name="idType">

    <xsd:restriction base="xsd:string">

    <xsd:pattern value="\d{1}-\d{4}-\d{4}-\d{1}"/>

    </xsd:restriction>

    </xsd:simpleType>

    idType是一个自定义的简单类型。

    我们对它做了限制:

    <xsd:restriction base="xsd:string">代表它是基于一个字符串类型。再用pattern元素来描述该字符串的形式。

    value="\d{1}-\d{4}-\d{4}-\d{1}"这是一个正则表达式,关于正则表达式,以后再介绍。嘻嘻!

    利用这个自定义的简单类型,我们可以重新写Schema文档:

    order4.xsd

    ---------------

    <?xml version="1.0"?>

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <xsd:element name="order">

    <xsd:complexType>

    <xsd:sequence>

    <xsd:element ref="orderItem" maxOccurs="10"/>

    </xsd:sequence>

    </xsd:complexType>

    </xsd:element>

    <xsd:element name="orderItem">

    <xsd:complexType>

    <xsd:sequence>

    <xsd:element name="id" type="idType"/>

    <xsd:element name="quantity" type="xsd:integer"/>

    </xsd:sequence>

    </xsd:complexType>

    </xsd:element>

    <xsd:simpleType name="idType">

    <xsd:restriction base="xsd:string">

    <xsd:pattern value="\d{1}-\d{4}-\d{4}-\d{1}"/>

    </xsd:restriction>

    </xsd:simpleType>

    </xsd:schema>

    假如我们事先确定好ID只有3个,即只有3个ISBN是可选的,那怎么办?我们可以用enumeration元素来进行列举。

    <xsd:simpleType name="idType">

    <xsd:restriction base="xsd:string">

    <xsd:enumeration value="7-5058-3496-7"/>

    <xsd:enumeration value="7-5005-6450-3"/>

    <xsd:enumeration value="7-3020-6069-7"/>

    </xsd:restriction>

    </xsd:simpleType>

    再来看订购量quantity的值,如果我们设定其值必须在1-10之间,该怎么办呢?可以这些自定义一个简单类型。

    <xsd:simpleType name="quantityType">

    <xsd:restriction base="xsd:integer">

    <xsd:minInclusive value="1"/>

    <xsd:maxInclusive value="10"/>

    </xsd:restriction>

    </xsd:simpleType>

    其中,minInclusive,maxInclusive分别代表该类型的取值范围。

    所以最终修改后的Schema文档如下:

    order4-1.xsd

    ----------------------

    <?xml version="1.0"?>

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <xsd:element name="order">

    <xsd:complexType>

    <xsd:sequence>

    <xsd:element ref="orderItem" maxOccurs="10"/>

    </xsd:sequence>

    </xsd:complexType>

    </xsd:element>

    <xsd:element name="orderItem">

    <xsd:complexType>

    <xsd:sequence>

    <xsd:element name="id" type="idType"/>

    <xsd:element name="quantity" type="quantityType"/>

    </xsd:sequence>

    </xsd:complexType>

    </xsd:element>

    <xsd:simpleType name="idType">

    <xsd:restriction base="xsd:string">

    <xsd:enumeration value="7-5058-3496-7"/>

    <xsd:enumeration value="7-5005-6450-3"/>

    <xsd:enumeration value="7-3020-6069-7"/>

    </xsd:restriction>

    </xsd:simpleType>

    <xsd:simpleType name="quantityType">

    <xsd:restriction base="xsd:integer">

    <xsd:minInclusive value="1"/>

    <xsd:maxInclusive value="10"/>

    </xsd:restriction>

    </xsd:simpleType>

    </xsd:schema>

    8. 定义属性
    最后,我们再来讲讲元素的属性如何在Schema文档中定义。

    比如上面的order.xml实例文档中:

    <order>

    <orderItem id="7-5058-3496-7" />

    </order>

    对此,我们在Schema文档中采用一个attribute来定义:

    order.xsd

    ---------

    <xsd:element name="orderItem">

    <xsd:complexType>

    <xsd:sequence>  ←空元素

    </xsd:sequence> 

    <!--定义该元素属性-->

    <xsd:attribute name="id" type="xsd:string"/>

    </xsd:complexType>

    </xsd:element>

    那么,实例文档中该属性值是必须的还是可有可无的呢?我们可以这样限制:

    <xsd:attribute name="id" type="idType" use="required"/>

    这里我们讲id属性类型作为一种自定义数据类型idType。

    而且,用attribute元素的use属性来定义是否是必须的属性。

    required是必须值,optional是可选值,prohibited是无属性值。

    那么对于属性的缺省值,我们怎么定义呢?
    比如:

    <order>

    <orderItem id="4-8443-1780-6" quantity="3"/>

    </order>

    我们还可以用attribute元素的另一个属性default来定义:

    <xsd:attribute name="quantity" type="xsd:integer" default="1"/>

    所以,我们可以重新写出一个Schema文档:

    order2.xsd

    --------------

    <xsd:element name="orderItem">

    <xsd:complexType>

    <xsd:sequence></xsd:sequence>

    <xsd:attribute name="id" type="idType" use="required"/>

    <xsd:attribute name="quantity" type="xsd:integer" default="1"/>

    </xsd:complexType>

    </xsd:element>

    上面的属性我们定义我们还可以采用属性组的办法来重新改写Schema文档。

    order3.xsd

    ----------------

    <xsd:element name="orderItem">

    <xsd:complexType>

    <xsd:sequence></xsd:sequence>

    <xsd:attributeGroup ref="orderItemAttributes"/>

    </xsd:complexType>

    </xsd:element>

    <xsd:attributeGroup name="orderItemAttributes">

    <xsd:attribute name="id" type="idType" use="required"/>

    <xsd:attribute name="quantity" type="xsd:integer" default="1"/>

    </xsd:attributeGroup>

    这个属性组就不详细解释了,不过,大家一看就清楚了吧。

    最后,我们写一个完整的订书order.xml的Schema文档。

    <?xml version="1.0"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <xsd:element name="order">

    <xsd:complexType>

    <xsd:sequence>

    <xsd:element ref="orderItem" maxOccurs="10"/>

    </xsd:sequence>

    </xsd:complexType>

    </xsd:element>

    <xsd:element name="orderItem">

    <xsd:complexType>

    <xsd:sequence></xsd:sequence>

    <xsd:attributeGroup ref="orderItemAttributes"/>

    </xsd:complexType>

    </xsd:element>

    <xsd:attributeGroup name="orderItemAttributes">

    <xsd:attribute name="id" type="idType" use="required"/>

    <xsd:attribute name="quantity" type="xsd:integer" default="1"/>

    </xsd:attributeGroup>

    <xsd:simpleType name="idType">

    <xsd:restriction base="xsd:string">

    <xsd:pattern value="\d{1}-\d{4}-\d{4}-\d{1}"/>

    </xsd:restriction>

    </xsd:simpleType>

    </xsd:schema>

    其他xml中引用xsd

    <?xml version="1.0" encoding="utf-8" standalone="no"?>

    <Envlope xmlns="gmip" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="gmip_appraise_rep.xsd">

    Xsd中对应为:

    <?xml version="1.0"?>

    <xsd:schema targetNamespace="gmip" xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified">

    ElementFormDefault=”qualified” 意思是要求element使用的namespace是targetNamespace,它的作用是对元素起"限定与非限定"使用,意思是在文档范例中要求采用命名空间前缀。

    作者Blog:http://blog.csdn.net/key/


       收藏   分享  
    顶(0)
      




    ----------------------------------------------
    没人帮忙,那就靠自己,自己才是最好的老师!本人拒绝回答通过站内短消息提出的问题!

    blog:http://Qr.blogger.org.cn

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2005/1/29 12:17:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 DTD/XML Schema 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/4/18 9:47:08

    本主题贴数19,分页: [1] [2]

     *树形目录 (最近20个回帖) 顶端 
    主题:  [转帖]XML Schema教程(13146字) - Qr,2005年1月29日
        回复:  (2字) - benben1234,2005年7月13日
        回复:  不错,刚刚拜读!(16字) - mallory,2005年6月15日
        回复:  謝謝!太棒暸(12字) - 邪之正義,2005年5月27日
        回复:  谢谢,我是个菜鸟,看这个文档有些了解了,呵呵(44字) - wzhg12345,2005年5月25日
        回复:  非常好的帖子,支持(18字) - songqianyong,2005年5月23日
        回复:  谢过////////////(16字) - boolean,2005年5月19日
        回复:  学习中.谢谢了(13字) - nickchan,2005年5月19日
        回复:  谢谢(6字) - kua,2005年5月12日
        回复:  非常感谢(8字) - 23019823,2005年5月10日
        回复:  好东西!谢谢!收藏ing(19字) - kenko,2005年5月8日
        回复:  谢谢撒(6字) - cuimingda,2005年4月9日
        回复:  多谢了,正好需要这入门的东东(28字) - 火彩,2005年4月5日
        回复:  good(4字) - littlefly,2005年4月5日
        回复:  很是详细,而且很容易理解,好东西,收藏!(36字) - anchen0617,2005年4月3日
        回复:  在定义元素时通过 minOccurs 和 maxOccurs来控制该元素可以在xml文档中出现的最..(90字) - sardinescn,2005年3月31日
        回复:  我想问一下,当一个子元素中有好多孙元素,例如: <point> ..(604字) - hclown,2005年3月28日
        回复:  不错!(5字) - chx821,2005年3月28日
        回复:  很好的入门文章。(16字) - wuenlee,2005年3月25日

    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    156.250ms