xml基础知识(9)

来源:互联网 发布:ipad pro 10.5 知乎 编辑:程序博客网 时间:2024/05/17 03:54

xml文档中可以写什么元素,元素中又可以增加什么属性,这些限定都需要限定文件来给予约束。

所以有必要了解约束文档的写法。

约束文档有两种:DTD、Schema。

Schema可以引入多个约束文档,DTD只能引入一个。

此处讲解Schema的基础知识。


案例:


元素定义:
<xs:element name="lastname" type="xs:string" default="bbb"/>
<xs:element name="age" type="xs:integer" fixed="15"/>
<xs:element name="dateborn" type="xs:date" use="required"/>
对应的xml元素:
<lastname>aaa</lastname>
<age>15</age>
<dateborn>1999-09-09</dateborn>


限定属性:
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">//age只能写入integer类型
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>//限定在0-120之间
<xs:enumeration value="29"/>
<xs:enumeration value="69"/>//限定枚举
</xs:restriction>
</xs:simpleType>


<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z][A-Z]"/>//输入正则表达式约束
</xs:restriction>
</xs:simpleType>


定义复杂元素:
<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:element name="product" type="prodtype"/>
<xs:complexType name="prodtype">
<xs:attribute name="prodid" type="xs:positiveInteger"/>//属性名称为prodid,属性内容为正整数
</xs:complexType>


指示器:
<xs:element name="person">
<xs:complexType>
<xs:choice>
<xs:element name="employee" type="employee"/>
<xs:element name="member" type="member"/>//两个子元素二选一
</xs:choice>
</xs:complexType>
</xs:element>


<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string" maxOccurs="10" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>

原创粉丝点击