XML讲义-20131016

前端之家收集整理的这篇文章主要介绍了XML讲义-20131016前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

第五章 XML Schema高级知识


5.1 使用anyType定义任意类型

当某个元素的类型是anyType时,就意味着该元素的内容不受任何限制。anyType是所有简单类型和复杂类型的基类型,它通常用于派生新的类型,而不是直接用来定义元素。

5.2 定义复杂类型

  • 复杂类型专门为元素准备---------只有元素的类型才能是复杂类型。属性的类型只能是简单类型而元素的类型既可以是简单类型,也可以是复杂类型
  • 元素内容是简单类型值,但元素包含属性---------称为包含简单内容的复杂类型
  • 包含子元素的元素,空元素或混合内容的元素,不管他们是否包含属性,都是复杂类型---------称为包含复杂内容的复杂类型
  • 定义复杂类型使用<complexType.../>元素,它可以接受两个子元素:
  • <simpleContent.../>用于定义包含简单内容的复杂类型
  • <complexContent.../>用于定义包含复杂内容的复杂类型
  • @H_404_22@

    5.2.1 定义复杂类型的方式

    <simpleContent.../>元素有且只能有一个如下的子元素:

    • <restriction.../>:对已有类型进行限制从而派生新的复杂类型
    • <extension.../>:对已有类型进行扩展从而派生新的复杂类型
    • @H_404_22@

      5.2.2 扩展简单类型

      使用<extension.../>能以扩展简单类型的方式来派生复杂类型,这种扩展包括添加属性属性组等方式

      extendSimpleType.xsd

      <?xml version="1.0" encoding="UTF-8"?>
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
      	<!-- 定义一个book_Type类型 -->
      	<xs:complexType name="book_Type">
      		<xs:simpleContent>
      			<xs:extension base="xs:decimal">
      				<xs:attribute name="isbn" type="xs:token"/>
      				<xs:attribute name="name" type="xs:token"/>
      			</xs:extension>
      			<!-- 以decimal类型为基础扩展新类型 -->
      			<!-- 增加两个属性 -->
      		</xs:simpleContent>
      	</xs:complexType>
      	<xs:element name="book" type="book_Type"/>
      </xs:schema>


      extendSimpleType.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      	xsi:noNamespaceSchemaLocation="extendSimpleType.xsd"
      	isbn="123456" name="疯狂Java讲义">
      	12.34
      </book>
      


      不要试图限制简单类型来派生复杂类型,为简单类型增加限制只能派生出新的简单类型

      5.2.3 包含属性的两种方式

      • 局部属性:直接将属性定义在<complexType.../>内定义,该局部属性只能属于当前复杂类型
      • 全局属性:<attribute.../>元素指定ref属性建立全局属性和复杂属性之间的关联
      • <attribute.../>属性fixde:为该属性指定一个固定值
      • default:为该属性指定一个默认值
      • @H_404_22@

        localAttribute.xsd

        <?xml version="1.0" encoding="UTF-8"?>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
        	<!-- 定义一个book_Type类型 -->
        	<xs:complexType name="book_Type">
        		<xs:simpleContent>
        			<xs:extension base="xs:decimal">
        				<xs:attribute name="isbn" default="123456">
        					<!-- 使用simpleType子元素指定属性类型 -->
        					<xs:simpleType>
        						<xs:restriction base="xs:int">
        							<xs:totalDigits value="8"/>
        						</xs:restriction>
        					</xs:simpleType>
        				</xs:attribute>
        				<xs:attribute name="name" type="xs:token"/>
        			</xs:extension>
        			<!-- 以decimal类型为基础扩展新类型 -->
        			<!-- 定义一个局部属性(属于当前复杂类型),没有指定type属性 -->
        			<!-- 定义一个局部属性(属于当前复杂类型),通过type属性指定属性类型 -->
        		</xs:simpleContent>
        	</xs:complexType>
        </xs:schema>


        globalAttribute.xsd

        <?xml version="1.0" encoding="UTF-8"?>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
        	<!-- 定义一个book_Type类型 -->
        	<xs:complexType name="book_Type">
        		<xs:simpleContent>
        			<xs:extension base="xs:decimal">
        				<xs:attribute ref="isbn" use="optional"/>
        				<xs:attribute ref="name" use="prohibited"/>
        			</xs:extension>
        			<!-- 以decimal类型为基础扩展新类型 -->
        			<!-- 通过ref引用已有的isbn属性,指定该属性是可选的 -->
        			<!-- 通过ref引用已有的isbn属性,指定该属性是可选的 -->
        		</xs:simpleContent>
        	</xs:complexType>
        	<!-- 定义一个全局属性(可属于任何复杂类型),没有指定type属性 -->
        	<xs:attribute name="isbn" default="123456">
        		<!-- 使用simpleType子元素指定属性类型 -->
        		<xs:simpleType>
        			<xs:restriction base="xs:int">
        				<xs:totalDigits value="8"/>
        			</xs:restriction>
        		</xs:simpleType>
        	</xs:attribute>
        	<!-- 定义一个全局属性(属于任何复杂类型),通过type属性指定属性类型 -->
        	<xs:attribute name="name" type="xs:token"/>
        </xs:schema>


        只有在如下两种情况下才能为<attribute.../>元素指定use属性

        • <attribute.../>元素里指定了ref属性
        • 采用<attribute.../>元素定义局部属性
        • @H_404_22@

          use属性用于指定该复杂类型对该属性的要求

          • optional:指定该属性时可选的
          • prohibited:指定该属性是被禁止的,表明该属性不能出现
          • required:指定该属性时必须的
          • @H_404_22@

            5.2.4 扩展包含简单内容的复杂类型

            使用的依然是<simpleContent.../>元素,因此扩展后的类型依然只能是包含简单内容的复杂类型。简单的说,这种扩展方式只能是添加属性

            extendSimpleContent.xsd

            <?xml version="1.0" encoding="UTF-8"?>
            <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
            	<!-- 定义一个book_Type类型 -->
            	<xs:complexType name="book_Type">
            		<xs:simpleContent>
            			<xs:extension base="xs:token">
            				<xs:attribute name="name" type="xs:token" use="required"/>
            				<xs:attribute name="isbn" use="required">
            					<!-- 使用simpleType子元素定义isbn属性的类型 -->
            					<xs:simpleType>
            						<xs:restriction base="xs:int">
            							<xs:totalDigits value="8"/>
            						</xs:restriction>
            					</xs:simpleType>
            				</xs:attribute>
            			</xs:extension>
            			<!-- 从token类型派生出book_Type类型 -->
            			<!-- 增加一个name属性 -->
            			<!-- 增加一个isbn属性 -->
            		</xs:simpleContent>
            	</xs:complexType>
            	<!-- 定义一个新的extended_book_Type类型 -->
            	<xs:complexType name="extended_book_Type">
            		<xs:simpleContent>
            			<xs:extension base="book_Type">
            				<xs:attribute name="price" use="required">
            					<!-- 使用simpleType子元素定义price属性的类型 -->
            					<xs:simpleType>
            						<xs:restriction base="xs:decimal">
            							<xs:maxExclusive value="100"/>
            							<xs:minExclusive value="0"/>
            							<xs:fractionDigits value="2"/>
            						</xs:restriction>
            					</xs:simpleType>
            				</xs:attribute>
            			</xs:extension>
            			<!-- 从book_Type类型派生出extended_book_Type类型 -->
            			<!-- 增加price属性 -->
            		</xs:simpleContent>
            	</xs:complexType>
            	<!-- 定义book元素,其类型是extended_book_Type -->
            	<xs:element name="book" type="extended_book_Type"/>
            </xs:schema>


            extendSimpleContent.xml

            <?xml version="1.0" encoding="UTF-8"?>
            <book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            	xsi:noNamespaceSchemaLocation="extendSimpleContent.xsd"
            	isbn="12345"
            	name="疯狂Java讲义"
            	price="99.00"/>


            5.2.5 限制包含简单内容的复杂类型

            限制包含简单内容的复杂类型主要可从3个方面入手:

            • 为元素内容增加进一步的约束
            • 为元素的属性类型增加进一步的约束
            • 删除某些元素
            • @H_404_22@

              不要试图限制简单类型来派生复杂类型,为简单类型增加限制只能派生出新的简单类型

              restrictSimpleContent.xsd

              <?xml version="1.0" encoding="UTF-8"?>
              <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
              	<!-- 定义一个book_Type类型 -->
              	<xs:complexType name="book_Type">
              		<xs:simpleContent>
              			<xs:extension base="xs:token">
              				<xs:attribute name="name" type="xs:token" use="required"/>
              				<xs:attribute name="isbn">
              					<!-- 使用simpleType子元素定义isbn属性的类型 -->
              					<xs:simpleType>
              						<xs:restriction base="xs:int">
              							<xs:totalDigits value="8"/>
              						</xs:restriction>
              					</xs:simpleType>
              				</xs:attribute>
              			</xs:extension>
              		</xs:simpleContent>
              	</xs:complexType>
              	<!-- 定义一个新的restricted_book_Type类型 -->
              	<xs:complexType name="restricted_book_Type">
              		<xs:simpleContent>
              			<xs:restriction base="book_Type">
              				<xs:enumeration value="疯狂Java体系"/>
              				<xs:enumeration value="疯狂Java实训教程"/>
              				<xs:attribute name="name" use="required">
              					<!-- 使用simpleType重新限定name属性的类型 -->
              					<xs:simpleType>
              						<xs:restriction base="xs:token">
              							<xs:maxLength value="14"/>
              							<xs:minLength value="4"/>
              						</xs:restriction>
              					</xs:simpleType>
              				</xs:attribute>
              				<xs:attribute name="isbn" use="prohibited"/>
              			</xs:restriction>
              			<!-- 定义该元素的内容只能是如下枚举值之一 -->
              			<!-- 删除原来的isbn属性 -->
              		</xs:simpleContent>
              	</xs:complexType>
              	<!-- 定义book元素,其类型是restricted_book_Type -->
              	<xs:element name="book" type="restricted_book_Type"/>
              </xs:schema>


              restrictSimpleContent.xml

              <?xml version="1.0" encoding="UTF-8"?>
              <book name="疯狂Ajax讲义" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              	xsi:noNamespaceSchemaLocation="restrictSimpleContent.xsd">
              疯狂Java体系
              </book>


              5.2.6 限制antType派生新类型

              anyType是所有类型的基类型,因此通过限制anyType来派生新的类型应使用<complexContent.../>子元素。也就是说,它派生出来的是包含复杂内容的复杂类型,这就可以定义包含子元素的元素类型了

              Schema为定义子元素提供了如下3个元素:

              • <sequence.../>元素:该元素包含的所有子元素必须按定义的顺序出现
              • <choice.../>元素:该元素包含的所有子元素只能出现其中之一
              • <all.../>元素:该元素包含的子元素能以任意顺序出现
              • @H_404_22@

                restrictAnyType.xsd

                <?xml version="1.0" encoding="UTF-8"?>
                <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
                	<xs:complexType name="book_Type">
                		<xs:complexContent>
                			<xs:restriction base="xs:anyType">
                				<xs:sequence>
                					<xs:element name="name" type="xs:string"/>
                					<xs:element name="price" type="xs:decimal"/>
                				</xs:sequence>
                				<xs:attribute name="isbn" type="xs:int" use="required"/>
                			</xs:restriction>
                			<!-- 通过限制anyType进行派生 -->
                			<!-- 使用sequence定义两个有序的子元素 -->
                			<!-- 定义一个isbn属性 -->
                		</xs:complexContent>
                	</xs:complexType>
                	<xs:complexType name="game_Type">
                		<xs:complexContent>
                			<xs:restriction base="xs:anyType">
                				<xs:sequence>
                					<xs:element name="name" type="xs:string"/>
                					<xs:element name="type" type="xs:string"/>
                				</xs:sequence>
                			</xs:restriction>
                			<!-- 通过限制anyType进行派生 -->
                			<!-- 使用sequence定义两个有序的子元素 -->
                		</xs:complexContent>
                	</xs:complexType>
                	<xs:complexType name="favorite-list_Type">
                		<xs:complexContent>
                			<xs:restriction base="xs:anyType">
                				<xs:choice>
                					<xs:element name="book" type="book_Type"/>
                					<xs:element name="game" type="game_Type"/>
                				</xs:choice>
                			</xs:restriction>
                			<!-- 通过限制anyType进行派生 -->
                			<!-- 使用choice定义两个互斥的子元素 -->
                		</xs:complexContent>
                	</xs:complexType>
                	<xs:element name="favorite-list" type="favorite-list_Type"/>
                </xs:schema>


                restrictAnyType1.xml

                <?xml version="1.0" encoding="UTF-8"?>
                <favorite-list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                	 xsi:noNamespaceSchemaLocation="restrictAnyType.xsd">
                	<book isbn="12345">
                		<name>疯狂Java讲义</name>
                		<price>99.00</price>
                	</book>
                </favorite-list>

                restrictAnyType2.xml

                <?xml version="1.0" encoding="UTF-8"?>
                <favorite-list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                	 xsi:noNamespaceSchemaLocation="restrictAnyType.xsd">
                	<game>
                		<name>侍魂</name>
                		<type>动作格斗</type>
                	</game>
                </favorite-list>
                


                Schema约定:如果某个复杂类型是由限制anyType派生出来的,那么在定义该复杂类型时可以省略<complexType.../>和<restriction.../>元素,而直接在<complexType.../>元素内使用<sequence.../>、<choice.../>、<all.../>和<attribute.../>来定义元素和属性

                restrictAnyType2.xsd

                <?xml version="1.0" encoding="UTF-8"?>
                <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
                	<xs:complexType name="book_Type">
                		<xs:sequence>
                			<xs:element name="name" type="xs:string"/>
                			<xs:element name="price" type="xs:decimal"/>
                		</xs:sequence>
                		<xs:attribute name="isbn" type="xs:int" use="required"/>
                		<!-- 直接使用sequence定义两个有序的子元素 -->
                		<!-- 直接定义一个isbn属性 -->
                	</xs:complexType>
                	<xs:complexType name="game_Type">
                		<xs:sequence>
                			<xs:element name="name" type="xs:string"/>
                			<xs:element name="type" type="xs:string"/>
                		</xs:sequence>
                		<!-- 直接使用sequence定义两个有序的子元素 -->
                	</xs:complexType>
                	<xs:complexType name="favorite-list_Type">
                		<xs:choice>
                			<xs:element name="book" type="book_Type"/>
                			<xs:element name="game" type="game_Type"/>
                		</xs:choice>
                		<!-- 直接使用choice定义两个互斥的子元素 -->
                	</xs:complexType>
                	<xs:element name="favorite-list" type="favorite-list_Type"/>
                </xs:schema>

                5.2.7 包含子元素的两种方式

                • 全局元素:将<element.../>元素放在<schema.../>元素的根元素下定义
                • 局部元素:将<element.../>元素放在<sequence.../>、<choice.../>或<all.../>等3个元素里定义
                • @H_404_22@

                  使用<element.../>元素时还可以使用minOccurs和maxOccurs两个属性,但只有在如下两种情况下才能为<element.../>元素指定minOccurs和maxOccurs属性

                  • <element.../>元素里指定了ref属性
                  • 采用<element.../>元素定义局部元素时
                  • @H_404_22@

                    minOccurs和maxOccurs属性值分别用于指定该元素允许出现的最小次数和最大次数,它们可以是任何非零整数或者unbounded(不限制最大次数

                    frequency.xsd

                    <?xml version="1.0" encoding="UTF-8"?>
                    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                    	elementFormDefault="qualified" attributeFormDefault="unqualified">
                    	<!-- 定义一个favorite-list_Type复杂类型 -->
                    	<xs:complexType name="favorite-list_Type">
                    		<xs:sequence>
                    			<!-- 如下三个子元素指定了出现频率 -->
                    			<xs:element name="book" type="xs:token"
                    				minOccurs="2" maxOccurs="unbounded"/>
                    			<xs:element name="game" type="xs:token"
                    				minOccurs="0" maxOccurs="2"/>
                    			<xs:element name="sport" type="xs:token"
                    				minOccurs="1" maxOccurs="1"/>
                    		</xs:sequence>
                    	</xs:complexType>
                    	<!-- 定义一个favorite-list元素 -->
                    	<xs:element name="favorite-list" type="favorite-list_Type"/>
                    </xs:schema>
                    

                    frequency.xml

                    <?xml version="1.0" encoding="UTF-8"?>
                    <favorite-list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    	 xsi:noNamespaceSchemaLocation="frequency.xsd">
                    	<!-- book元素最少出现2次 -->
                    	<book>疯狂Java讲义</book>
                    	<book>疯狂Ajax讲义</book>
                    	<!-- game元素可以不出现,可以出现1次 -->
                    	<game>侍魂</game>
                    	<!-- sport元素必须出现1次,只能出现1次 -->
                    	<sport>乒乓球</sport>
                    </favorite-list>
                    


                    除此之外,还可以为<sequence.../>、<choice.../>、<all.../>等元素指定这两个属性,用以表示整个元素组允许出现的次数。特别是当需要指定无序子元素时(<all.../>元素的功能太有限,它所包含的每一个无序子元素最多只能出现一次),此时可以为<choice.../>元素增加maxOccurs属性来实现

                    noSequence.xsd

                    <?xml version="1.0" encoding="UTF-8"?>
                    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                    	 elementFormDefault="qualified" attributeFormDefault="unqualified">
                    	<xs:element name="favorite-list">
                    		<xs:complexType>
                    			<!-- 定义整个choice组可出现无限次 -->
                    			<xs:choice maxOccurs="unbounded">
                    				<!-- 如下三个元素可出现任意之一 -->
                    				<xs:element name="book" type="xs:token"/>
                    				<xs:element name="game" type="xs:token"/>
                    				<xs:element name="sport" type="xs:token"/>
                    			</xs:choice>
                    		</xs:complexType>
                    	</xs:element>
                    </xs:schema>
                    


                    noSequence.xml

                    <?xml version="1.0" encoding="UTF-8"?>
                    <favorite-list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                      xsi:noNamespaceSchemaLocation="noSequence.xsd">
                     <!-- 如下三种子元素是无序的、且可出现多次 -->
                     <game>雷电</game>
                     <book>庄子</book>
                     <game>侍魂</game>
                     <book>诗经</book>
                     <sport>乒乓球</sport>
                    </favorite-list>


                    5.2.8 空元素类型

                    空元素类型用于定义元素内容为空或空字符串的元素,但该元素可以接受属性

                    定义空元素类型方式:

                    • 限制anyType:限制anyType时不定义任何子元素,只定义属性即可
                    • @H_404_22@

                      empty2.xsd

                      <?xml version="1.0" encoding="UTF-8"?>
                      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
                      	elementFormDefault="qualified" attributeFormDefault="unqualified">
                      	<xs:element name="book">
                      		<xs:complexType>
                      			<!-- 不指定任何子元素,也就是该元素内容为空 -->
                      			<!-- 新增一个name属性 -->
                      			<xs:attribute name="name" type="xs:token"/>
                      		</xs:complexType>
                      	</xs:element>
                      </xs:schema>
                      


                      5.3 复杂类型的进一步派生

                      Schema没有内置任何复杂类型,所有复杂类型都是由开发者派生而来

                      5.3.1 限制空元素类型

                      对于空元素类型而言,元素不包含任何内容,但可以拥有属性。因此限制空元素类型时可以从如下两方面入手:

                      • 对指定属性增加进一步约束
                      • 删除某个属性
                      • @H_404_22@

                        restrict_empty.xsd

                        <?xml version="1.0" encoding="UTF-8"?>
                        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                        	elementFormDefault="qualified" attributeFormDefault="unqualified">
                        	<xs:complexType name="book_Type">
                        		<!-- 新增一个name属性 -->	
                        		<xs:attribute name="name" type="xs:token"/>
                        		<!-- 定义一个price属性,以simpleType子元素指定类型 -->
                        		<xs:attribute name="price">
                        			<xs:simpleType>
                        				<!-- 以decimal为基础,派生新的简单类型 -->
                        				<xs:restriction base="xs:decimal">
                        					<xs:fractionDigits value="2"/>
                        				</xs:restriction>
                        			</xs:simpleType>
                        		</xs:attribute>
                        	</xs:complexType>
                        	<xs:complexType name="retricted_book_Type">
                        		<xs:complexContent>
                        			<!-- 通过限制空类型派生新类型  -->
                        			<xs:restriction base="book_Type">
                        				<!-- 对原有的name属性增加进一步约束 -->
                        				<xs:attribute name="name">
                        					<xs:simpleType>
                        						<xs:restriction base="xs:token">
                        							<xs:maxLength value="12"/>
                        							<xs:minLength value="4"/>
                        						</xs:restriction>
                        					</xs:simpleType>
                        				</xs:attribute>	
                        				<!-- 删除price属性 -->
                        				<xs:attribute name="price" use="prohibited"/>
                        			</xs:restriction>
                        		</xs:complexContent>
                        	</xs:complexType>
                        	<xs:element name="book" type="retricted_book_Type"/>
                        </xs:schema>
                        


                        对空元素类型增加限制派生出来的只能是空元素类型,只不过这种空元素类型要么具有更少的属性,要么其属性具有更严格的语义约束

                        5.3.2 扩展空元素类型

                        扩展空元素类型可以从如下3方面入手:

                        • 为原有类型增加属性:派生出来的新类型依然是空元素类型
                        • 为原有类型增加子元素:派生出来的新类型将是包含子元素的类型
                        • 为原有类型增加mixed=“true”:派生出来的新类型将是混合内容类型
                        • @H_404_22@

                          extend_empty.xsd

                          <?xml version="1.0" encoding="UTF-8"?>
                          <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
                          	elementFormDefault="qualified" attributeFormDefault="unqualified">
                          	<xs:complexType name="book_Type">
                          		<!-- 定义一个name属性,其类型是token -->	
                          		<xs:attribute name="name" type="xs:token"/>
                          		<!-- 定义一个price属性,以simpleType子元素指定类型 -->
                          		<xs:attribute name="price">
                          			<xs:simpleType>
                          				<!-- 以decimal为基础,派生新的简单类型 -->
                          				<xs:restriction base="xs:decimal">
                          					<xs:fractionDigits value="2"/>
                          				</xs:restriction>
                          			</xs:simpleType>
                          		</xs:attribute>
                          	</xs:complexType>
                          	<!-- 将其定义为混合类型 -->
                          	<xs:complexType name="extended_book_Type" mixed="true">
                          		<xs:complexContent>
                          			<!-- 通过扩展空类型派生新类型  -->
                          			<xs:extension base="book_Type">
                          				<!-- 使用sequence为原有类型新增两个子元素 -->
                          				<xs:sequence>
                          					<xs:element name="publish-house" type="xs:token"/>
                          					<xs:element name="publish-date" type="xs:date"/>
                          				</xs:sequence>
                          				<xs:attribute name="isbn" type="xs:int" use="required"/>
                          			</xs:extension>
                          		</xs:complexContent>
                          	</xs:complexType>
                          	<xs:element name="book" type="extended_book_Type"/>
                          </xs:schema>
                          


                          extend_empty.xml

                          <?xml version="1.0" encoding="UTF-8"?>
                          <book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                          	xsi:noNamespaceSchemaLocation="extend_empty.xsd"
                          	isbn="12344"
                          	name="疯狂Java讲义"
                          	price="99.00">
                          	<publish-house>电子工业出版社</publish-house>
                          	<publish-date>2008-10-20</publish-date>
                          </book>


                          5.3.3 限制包含子元素的类型

                          限制包含子元素的类型可以从如下几个方面入手:

                          • 可以对指定属性的类型增加进一步约束
                          • 可以对指定子元素的类型增加进一步约束
                          • 可以删除指定属性
                          • 可以删除指定元素
                          • @H_404_22@

                            派生类型如果需要保留基类型的子元素,则必须重新定义这些子元素,否则即可认为删除了这些子元素。如果要删除指定属性,则必须在定义该属性时使用use="prohibited"。以限制方式来派生新类型时,Schema对基类型的子元素和属性的处理方式是不一样的:派生类型默认会删除基类型中的所有子元素定义,但会保留其中的所有属性定义

                            restrict_sequence.xsd

                            <?xml version="1.0" encoding="UTF-8"?>
                            <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                            	elementFormDefault="qualified" attributeFormDefault="unqualified">
                            	<!-- 定义一个book_Type类型,该类型下包含2个有序子元素 -->
                            	<xs:complexType name="book_Type">
                            		<!-- 使用sequence定义2个子元素 -->
                            		<xs:sequence>
                            			<xs:element name="name" type="xs:token"/>
                            			<!-- 如果派生类型想删除如下子元素,必须指定minOccurs="1" -->
                            			<xs:element name="price" type="xs:decimal" minOccurs="0"/>
                            		</xs:sequence>
                            		<!-- 为该类型定义2个属性 -->		
                            		<xs:attribute name="isbn" type="xs:int"/>
                            		<xs:attribute name="publish-date" type="xs:date"/>
                            	</xs:complexType>
                            	<!-- 定义restrict_book_Type类型 -->
                            	<xs:complexType name="restrict_book_Type">
                            		<xs:complexContent>
                            			<!-- 通过限制book_Type类型派生新类型 -->
                            			<xs:restriction base="book_Type">
                            				<xs:sequence>
                            					<!-- 为name元素的类型增加进一步约束 -->
                            					<xs:element name="name">
                            						<xs:simpleType>
                            							<xs:restriction base="xs:token">
                            								<xs:maxLength value="20"/>
                            								<xs:minLength value="4"/>
                            							</xs:restriction>
                            						</xs:simpleType>
                            					</xs:element>
                            					<!-- 不再定义price元素,即可认为删除了该元素 -->
                            				</xs:sequence>
                            				<!-- 为publish-date属性的类型增加进一步约束 -->
                            				<xs:attribute name="publish-date">
                            					<xs:simpleType>
                            						<xs:restriction base="xs:date">
                            							<xs:maxExclusive value="2009-05-12"/>
                            							<xs:minExclusive value="2007-05-12"/>
                            						</xs:restriction>
                            					</xs:simpleType>			
                            				</xs:attribute>
                            				<!-- 删除isbn属性 -->
                            				<xs:attribute name="isbn" use="prohibited"/>
                            			</xs:restriction>
                            		</xs:complexContent>
                            	</xs:complexType>	
                            	<xs:element name="book" type="restrict_book_Type"/>
                            </xs:schema>
                            


                            restrict_sequence.xml

                            <?xml version="1.0" encoding="UTF-8"?>
                            <book publish-date="2008-10-12" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                            	xsi:noNamespaceSchemaLocation="restrict_sequence.xsd">
                            	<name>疯狂Java讲义</name>
                            </book>
                            


                            5.3.4 扩展包含子元素的类型

                            扩展包含子元素的类型可从如下两个方面入手:

                            • 为基类型增加新的子元素
                            • 为基类型增加新的属性
                            • @H_404_22@

                              当为基类型添加新的子元素时,Schema使用<sequence.../>元素来组合基类型中原有的子元素组和新增的子元素组

                              extend_sequence.xsd

                              <?xml version="1.0" encoding="UTF-8"?>
                              <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                              	elementFormDefault="qualified" attributeFormDefault="unqualified">
                              	<!-- 定义一个book_Type类型,该类型下包含2个有序子元素 -->
                              	<xs:complexType name="book_Type">
                              		<!-- 使用sequence定义2个子元素 -->
                              		<xs:sequence>
                              			<xs:element name="name" type="xs:token"/>
                              			<xs:element name="price" type="xs:decimal"/>
                              		</xs:sequence>
                              		<!-- 为该类型定义2个属性 -->		
                              		<xs:attribute name="isbn" type="xs:int"/>
                              		<xs:attribute name="publish-date" type="xs:date"/>
                              	</xs:complexType>
                              	<!-- 定义extend_book_Type类型 -->
                              	<xs:complexType name="extend_book_Type">
                              		<xs:complexContent>
                              			<!-- 通过扩展book_Type类型派生新类型 -->
                              			<xs:extension base="book_Type">
                              				<xs:sequence>
                              					<!-- 新增定义2个子元素 -->
                              					<xs:element name="type" type="xs:token"/>
                              					<xs:element name="targetMarket" type="xs:token"/>
                              				</xs:sequence>
                              				<!-- 新增一个publish-house属性 -->
                              				<xs:attribute name="publish-house" type="xs:token"/>
                              			</xs:extension>
                              		</xs:complexContent>
                              	</xs:complexType>	
                              	<xs:element name="book" type="extend_book_Type"/>
                              </xs:schema>
                              


                              extend_sequence.xml

                              <?xml version="1.0" encoding="UTF-8"?>
                              <book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                              	xsi:noNamespaceSchemaLocation="extend_sequence.xsd"
                              	publish-date="2008-10-12"
                              	isbn="12445" 
                              	publish-house="电子工业出版社">
                              	<name>疯狂Java讲义</name>
                              	<price>99.00</price>
                              	<type>IT编程图书</type>
                              	<targetMarket>想系统学习Java的学习、工作者</targetMarket>
                              </book>
                              

猜你在找的XML相关文章