2016年11月14日 星期一

Schema標籤用法

參考資料

※simpleType、complexType

※test.xsd

<element name="root">
    <complexType>
        <sequence>
            <element name="aaa" type="date" />
            <element name="bbb" type="boolean" default="false" />
            <element name="ccc" type="decimal" fixed="20.0" />
            <element name="ddd" type="bruce:sType1" />
            <element name="eee" type="bruce:sType2" />
    
            <element name="fff">
                <simpleType>
                    <restriction base="integer">
                        <pattern value="[0-9]{3}" />
                    </restriction>
                </simpleType>
            </element>
            
            <element name="ggg" type="bruce:cType" />
        </sequence>
    </complexType>
</element>
    
<simpleType name="sType1">
    <restriction base="int">
        <minInclusive value="100" />
        <maxExclusive value="1000" />
    </restriction>
</simpleType>
    
<simpleType name="sType2">
    <restriction base="string">
        <minLength value="2" />
        <maxLength value="4" />
    </restriction>
</simpleType>
    
<complexType name="cType">
    <sequence>
        <element name="g1" type="string" />
        <element name="g2" type="string" />
    </sequence>
</complexType>

※上一篇有說明命名空間了,所以只把必要的程式碼列出

※type有很多,但最長用的有6個,分別是string、decimal、integer、boolean、date、time

※pattern是正則表達式

※sequence表示元素要按照順序出現;如果不要順序,可以用all

※sType1是針對數字範圍的設定
include表示有包括0、exclude表示排除0,所以
maxExclusive:>
maxInclusive:>=
minExclusive:<
minInclusive:<=
會自動有min不能大於max的檢核


※sType2是針對字串範圍的設定,有3種
minLength、maxLength:<=、>=
length:如果上面兩個設定一樣,例如長度<=2,而且>=2,那就設定這一個就好了

※test.xml

<?xml version="1.0" encoding="UTF-8"?>
<b:root xmlns:b="456" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="456 Test.xsd">
    
    <b:aaa>2010-10-12</b:aaa>
    <b:bbb>true</b:bbb>
    <b:ccc>20.0</b:ccc>
    <b:ddd>123</b:ddd>
    <b:eee>abc</b:eee>
    <b:fff>456</b:fff>
    <b:ggg>
        <b:g1>g1</b:g1>
        <b:g2>g2</b:g2>
    </b:ggg>
</b:root>

※ddd和fff的檢核其實是一樣的



※空元素、屬性

※Test.xsd

<element name="root">
    <complexType>
        <sequence minOccurs="1" maxOccurs="1" >
    
            <element name="aaa">
                <complexType>
                    <attribute name="attr1" type="nonPositiveInteger" use="required" />
                </complexType>
            </element>
    
            <element name="bbb">
                <complexType>
                    <complexContent>
                        <restriction base="bruce:cType" />
                    </complexContent>
                </complexType>
            </element>
    
            <element name="ccc" type="bruce:cType" />
        </sequence>
    </complexType>
</element>
    
<complexType name="cType">
    <attribute name="attr2" type="nonNegativeInteger" use="required" />
</complexType>



※Test.xml

<bruce:root xmlns:bruce="456"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="456 Test.xsd ">
    
    <bruce:aaa attr1="-1" />
    <bruce:bbb attr2="1" />
    <bruce:ccc attr2="1" />
</bruce:root>

※屬性的use有三種:
optional:預設,表示可選的
required:一定要有
prohibited:一定不能有

※type的數字有四種,non開頭表示有包括0,所以
nonPositiveInteger:負整數,有包括0
nonNegativeInteger:正整數,有包括0
positiveInteger:正整數,不包括0
negativeInteger:負整數,不包括0

※minOccurs和maxOccurs預設都是1,表示以下的元素只能出現一次
unbounded是無限多次,也就是*的意思



※元素的多選一、屬性組、元素組

※Test.xsd

<element name="root">
    <complexType>
        <group ref="bruce:gp" />
        <attributeGroup ref="bruce:agp" />
    </complexType>
</element>
    
<group name="gp">
    <sequence>
        <element name="aaa" type="string" />
        <element name="bbb" type="string" />
        <element name="ccc">
            <complexType>
                <attribute name="cccAttr" type="date" use="required" />
            </complexType>
        </element>
        <choice>
            <element name="choice1" type="string" />
            <element name="choice2">
                <complexType>
                    <attribute name="c2attr" type="string" use="required" />
                </complexType>
            </element>
            <element name="choice3" type="boolean" />
        </choice>
    </sequence>
</group>
    
<attributeGroup name="agp">
    <attribute name="attr1" type="string" use="required" />
    <attribute name="attr2" type="int" use="required" />
    <attribute name="attr3" type="boolean" use="required" />
</attributeGroup>

※group是設定元素組的地方

※attributeGroup是設定屬性組的地方

※choice裡面的元素是多選一

※Test.xml

<?xml version="1.0" encoding="UTF-8"?>
<b:root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="456 Test.xsd"
    attr1="xxx"
    attr2="0"
    attr3="true"
    xmlns:b="456">
    
    <b:aaa>b:aaa</b:aaa>
    <b:bbb>b:bbb</b:bbb>
    <b:ccc cccAttr="2001-01-01" />
    <b:choice2 c2attr="ooo" />
</b:root>




※延伸、限制

※Test.xsd

<element name="root">
    <complexType>
        <all>
            <element name="aaa">
                <complexType>
                    <simpleContent>
                        <extension base="integer">
                            <attribute name="attr1" type="bruce:allSize" use="required" />
                        </extension>
                    </simpleContent>
                </complexType>
            </element>
    
            <element ref="bruce:bbb" />
    
            <element name="ccc">
                <simpleType>
                    <restriction base="int">
                        <minExclusive value="100" />
                        <maxExclusive value="1000" />
                    </restriction>
                </simpleType>
            </element>
    
            <element name="ddd" type="bruce:allSize" />
        </all>
    </complexType>
</element>
    
<simpleType name="allSize">
    <restriction base="string">
        <enumeration value="S" />
        <enumeration value="M" />
        <enumeration value="L" />
        <enumeration value="XL" />
        <enumeration value="XXL" />
    </restriction>
</simpleType>
    
<complexType name="baseElement">
    <choice>
        <element name="base1" type="string" />
        <element name="base2" type="string" />
    </choice>
</complexType>
    
<element name="bbb">
    <complexType>
        <complexContent>
            <extension base="bruce:baseElement">
                <sequence>
                    <element name="base3" type="string" />
                    <element name="base4" type="string" />
                </sequence>
            </extension>
        </complexContent>
    </complexType>
</element>

※extension和restriction裡面可以是空的,也就是等於的意思

※aaa延伸integer的功能,還是int

※bbb延伸baseElement的二選一功能,變成134或234,complexContent才可以增加元素

※ccc是限制數字的範圍

※ddd是限制字串只能是5個其中之一


※Test.xml

<?xml version="1.0" encoding="UTF-8"?>
<b:root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:b="456"
    xsi:schemaLocation="456 Test.xsd">
    
    <b:aaa attr1="S">0</b:aaa>
    <b:bbb>
        <b:base1>one</b:base1>
        <b:base3>three</b:base3>
        <b:base4>four</b:base4>
    </b:bbb>
    <b:ccc>101</b:ccc>
    <b:ddd>S</b:ddd>
</b:root>





※any、anyAttribute

※Test.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="456"
    xmlns:bruce="456" 
    elementFormDefault="qualified">
    
    <element name="root">
        <complexType>
            <sequence>
                <element name="aaa">
                    <complexType>
    
                        <!-- <anyAttribute /> -->
    
                        <simpleContent>
                            <extension base="string">
                                <anyAttribute />
                            </extension>
                        </simpleContent>
    
                    </complexType>
                </element>
                <any minOccurs="1" maxOccurs="3" />
            </sequence>
    
            <anyAttribute />
    
        </complexType>
    </element>
</schema>

※註解那行改成下面的simpleContent,
如果不註解沒辦法在aaa設定型態,aaa的內容只能是空的
在aaa設定型態會出「Element 'aaa' has both a 'type' attribute and a 'anonymous type' child. Only one of these is allowed for an element.」的錯
所以改成simpleContent,下面用extension的base屬性指定型態


※Test2.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
    targetNamespace="+-"
    elementFormDefault="qualified">
    
    <element name="bbb" type="string" />
    <attribute name="bAttr" type="int" />
</schema>

※設定一個元素和屬性

※Test.xml

<?xml version="1.0" encoding="UTF-8"?>
<b:root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:b="456"
    xmlns:b2="+-"
    xsi:schemaLocation="456 Test.xsd +- Test2.xsd" 
    b2:bAttr="1">
    
    <b:aaa b2:bAttr="2">xxx</b:aaa>
    <b2:bbb>ooo</b2:bbb>
</b:root>

※如果將Test.xsd的any或anyAttrubute註解,將會報錯



※substitution

代換和禁止代換

※test.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="456"
    xmlns:bruce="456" 
    elementFormDefault="qualified">
    
    <element name="up" type="bruce:pType" />
    <element name="root" substitutionGroup="bruce:up" />
    
    <element name="p1" type="string" />
    <element name="p2" substitutionGroup="bruce:p1" />
    
    <element name="b1" type="string" block="substitution"/>
    <element name="b2" substitutionGroup="bruce:b1" />
    
    <complexType name="pType">
        <sequence>
            <element ref="bruce:p1" />
            <element ref="bruce:b1" />
        </sequence>
    </complexType>
</schema>



※test.xml

<?xml version="1.0" encoding="UTF-8"?>
<b:up xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:b="456"
    xsi:schemaLocation="456 Test.xsd">
    
    <b:p2></b:p2>
    <b:b1></b:b1>
</b:up>

※up和root可以互換;p1和p2也可以互換,但b1不能用b2替換,因為b1有設block="substitution",不給替換,所以打b2會有錯

沒有留言:

張貼留言