Schema 基础

本文介绍了一种使用XML Schema定义XML文档结构的方法。详细解释了如何通过全局元素、局部元素及属性来规范XML文档的结构,并展示了如何利用简单类型、复杂类型、组、继承等概念进行类型定义。

 

<?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>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值