<?xml version="1.0" encoding="UTF-8"?>
<!--
以schema开始,以schema结束
这个schema的声明方式:定义一个全局元素表达root元素,在元素类型中定义子元素。
还有一种定义方式,所有的元素都是全局的,在元素类型定义中引用全局元素
重用的几种方法:一、引用元素或组
二、导入 <xsd:include schemalocation=""/>
三、扩充或限制
<xsd:complexType name="Tname">
<xsd:complexContent>
<xsd:extension base="...">
扩充内容
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="Tname">
<xsd:complexContent>
<xsd:restriction base="...">
用maxOccus限制
<xsd:element name="..." type="xsd:string" maxOccus="0"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
-->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/sampleSchema"
xmlns="http://www.example.org/sampleSchema"
elementFormDefault="qualified">
<xsd:annotation>
<xsd:documentation xml:lang="en">
Purchase Order Schema for redzero
</xsd:documentation>
</xsd:annotation>
<xsd:element name="po" type="poType" />
<xsd:simpleType name="skuType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="d{3}-[a-zA-Z]{2}"></xsd:pattern>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="poType">
<xsd:sequence>
<xsd:element name="billTo" type="addressType1"></xsd:element>
<xsd:element name="shipTo" type="addressType2" />
<xsd:element name="order">
<!-- 匿名类型定义 -->
<xsd:complexType>
<xsd:sequence>
<xsd:element name="item" type="itemType"
maxOccurs="unbounded">
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:positiveInteger"
use="required">
</xsd:attribute>
<xsd:attribute name="submitted" type="xsd:date"
use="required">
</xsd:attribute>
</xsd:complexType>
<xsd:group name="addressType">
<xsd:sequence>
<xsd:element name="company" type="xsd:string"></xsd:element>
<xsd:element name="street" type="xsd:string"
maxOccurs="unbounded" />
<xsd:element name="city" type="xsd:string"></xsd:element>
<xsd:element name="state" type="xsd:string"></xsd:element>
<xsd:element name="postalCode" type="xsd:positiveInteger"></xsd:element>
</xsd:sequence>
</xsd:group>
<!-- 重用addressType,引用定义好的组,接近于编程语言的组合 -->
<xsd:complexType name="addressType1">
<xsd:sequence>
<xsd:group ref="addressType"></xsd:group>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:ID" use="required"></xsd:attribute>
</xsd:complexType>
<xsd:complexType name="addressType2">
<xsd:sequence>
<xsd:group ref="addressType"></xsd:group>
</xsd:sequence>
<xsd:attribute name="href" type="xsd:IDREF" use="required"></xsd:attribute>
</xsd:complexType>
<xsd:complexType name="itemType">
<xsd:sequence>
<xsd:element name="description" type="xsd:string"></xsd:element>
</xsd:sequence>
<xsd:attribute name="sku" type="skuType" use="required"></xsd:attribute>
<xsd:attribute name="quantity" type="xsd:positiveInteger"
use="required">
</xsd:attribute>
</xsd:complexType>
<!-- 另一种重用的方法,扩充,接近于编程语言的继承 -->
<!--
<xsd:complexType name="addressType3">
<xsd:complexContent >
<xsd:extension base="addressType" >
<xsd:attribute name="id" type="xsd:ID" use="required"></xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
-->
</xsd:schema>
本文介绍了一种使用XML Schema定义XML文档结构的方法。详细解释了如何通过全局元素、局部元素及属性来规范XML文档的结构,并展示了如何利用简单类型、复杂类型、组、继承等概念进行类型定义。
972

被折叠的 条评论
为什么被折叠?



