| « | Mar.2026 | » | | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | | | |
| 公告 |
| 暂无公告... |
| Blog信息 |
|
blog名称: 日志总数:15 评论数量:4 留言数量:0 访问次数:46139 建立时间:2005年4月21日 |

| |
|
[收藏] -- 作者:teiki 文章收藏
xyin2005 发表于 2005/4/21 10:02:51 |
| 本文转载自W3CHINA.ORG讨论区(BBS.W3CHINA.ORG) 原文链接作者:teiki以下为原文:跟我学XML Schema(9):自定义简单类型
如果内置简单类型的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---------------1:<?xml version="1.0"?>2:<xsd:schema xmlns:xsd="' target=_blank>http://www.w3.org/2001/XMLSchema">3:4: <xsd:element name="order">5: <xsd:complexType>6: <xsd:sequence>7: <xsd:element ref="orderItem" maxOccurs="10"/>8: </xsd:sequence>9: </xsd:complexType>10: </xsd:element>11:12: <xsd:element name="orderItem">13: <xsd:complexType>14: <xsd:sequence>15: <xsd:element name="id" type="idType"/>16: <xsd:element name="quantity" type="xsd:integer"/>17: </xsd:sequence>18: </xsd:complexType>19: </xsd:element>20: 21: <xsd:simpleType name="idType">22: <xsd:restriction base="xsd:string">23: <xsd:pattern value="\d{1}-\d{4}-\d{4}-\d{1}"/>24: </xsd:restriction>25: </xsd:simpleType>26: 27:</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----------------------1:<?xml version="1.0"?>2:<xsd:schema xmlns:xsd="' target=_blank>http://www.w3.org/2001/XMLSchema">3:4: <xsd:element name="order">5: <xsd:complexType>6: <xsd:sequence>7: <xsd:element ref="orderItem" maxOccurs="10"/>8: </xsd:sequence>9: </xsd:complexType>10: </xsd:element>11:12: <xsd:element name="orderItem">13: <xsd:complexType>14: <xsd:sequence>15: <xsd:element name="id" type="idType"/>16: <xsd:element name="quantity" type="quantityType"/>17: </xsd:sequence>18: </xsd:complexType>19: </xsd:element>20: 21: <xsd:simpleType name="idType">22: <xsd:restriction base="xsd:string">23: <xsd:enumeration value="7-5058-3496-7"/><xsd:enumeration value="7-5005-6450-3"/><xsd:enumeration value="7-3020-6069-7"/>26: </xsd:restriction>27: </xsd:simpleType>28: 29: <xsd:simpleType name="quantityType">30: <xsd:restriction base="xsd:integer">31: <xsd:minInclusive value="1"/>32: <xsd:maxInclusive value="10"/>33: </xsd:restriction>34: </xsd:simpleType>35:36:</xsd:schema><完>参与讨论本主题 |
|
|