编程资料集中营
 | 网站首页 | 文章中心 | 编程资料2 | 软件下载 | BT下载 | 八卦星闻 | 音乐在线 | 在线游戏 | 免费电影 | 进入问吧 | 
第三页, 6.内置简单类型图省略7.自定义简单类型如果内置简单类型的44种还不能满足要求,怎么办呢?下面学习自定义简单类型。(XML的扩展性充分体现在这里)例如这个实例文档:order4.xml-----------------<order><orderItem><id>7-5058-3496-7</id><quantity>5</quantity></orderItem></order>ID是一个标准的ISBN编码,我们怎么定义这个ISBN编码呢?<xsd:simpleTypename=id,
您现在的位置: 编程资料,学习资料,c,c++,vc,vc++,java,jsp,j2ee,j2me,asp,php >> 文章中心 >> XML 技术 >> 文章正文
【字体:
第三页   进入问吧

本站地址:http://www.bajiao123.com

作者:admin    文章来源:不详    点击数:    更新时间:2007-3-7    

第三页

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>
   

进入问吧

本站地址:http://www.bajiao123.com

文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • 高级搜索
    编程资料集中营