XML-Schema-02

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

1.编写xsd文件

<?xml version="1.0" encoding="UTF-8"?>
<schema 
			xmlns="http://www.w3.org/2001/XMLSchema"
            <!--声明命名空间,可自定义-->

targetNamespace="http://www.example.org/class"

xmlns:tns="http://www.example.org/class" elementFormDefault="qualified">

 
                   <!--include相同命名空间的student.xsd文件-->
<!--需要导入命名空间不通的xsd时需要用import-->
			 <include schemaLocation="student.xsd" ></include>
		
			<element name="class" >
			       <complexType>
			                <sequence>
			                       <element name="claName" type="string" ></element>
			                     
			                       <element name="students">
			                               <complexType>
			                                      <sequence minOccurs="1" maxOccurs="unbounded">
			                                             <element name="student" type="tns:studentType"></element>                                           
			                                      </sequence>
			                               </complexType>
			                       </element>
			                </sequence>	
			       </complexType>
			</element>
</schema>



<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
               targetNamespace="http://www.example.org/class" 
	        xmlns:tns="http://www.example.org/class" 
             elementFormDefault="qualified">
             
             <complexType name="studentType">
                   <sequence>
                          <element name="id" type="tns:idenfiType"> </element>
                          <element name="stuName" type="string"></element>   
                          <element name="age" type="tns:ageType"></element>
                   </sequence>
                 <!--元素的attribute必须定义在sequence之后-->
                <!--use:required表示attribute是必须的;   optional表示:可选的--></span>
                   <attribute name="sex" type="tns:sexType" use="required"></attribute>
             </complexType>
            
          <!--simpleType节点下不能有element,主要功能是组织约束条件-->
             <simpleType name="sexType">
                    <restriction base="string">
                        <!--枚举约束-->
                         <enumeration value="男"></enumeration>
                         <enumeration value="女"></enumeration>
                    </restriction>
             </simpleType>
             
             <simpleType name="idenfiType">
                   <restriction base="int"> </restriction>                  
             </simpleType>
             
             <simpleType name="ageType">
                   <restriction base="int">
                         <!--取值范围约束-->
                         <minInclusive value="20"></minInclusive>
                         <maxInclusive value="120"></maxInclusive>
                   </restriction>
             </simpleType>
</schema>


2.引用xml文件中引用xsd.
<?xml version="1.0" encoding="UTF-8"?>
<xsd:class
      <!--命名空间别名xsd可以自定义,默认为空-->
         xmlns:xsd="http://www.example.org/class"
       <!--下面这行格式固定,表示获取XMLSchema的对象-->
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.example.org/class" >
    <xsd:claName>wuwh</xsd:claName>
    
    <xsd:students>
         <xsd:student sex="男">
             <xsd:id>12</xsd:id>
             <xsd:stuName>wuwh</xsd:stuName>
             <xsd:age>21</xsd:age>
         </xsd:student>
         
           <xsd:student sex="女">
             <xsd:id>20</xsd:id>
             <xsd:stuName>wumz</xsd:stuName>
             <xsd:age>22</xsd:age>
         </xsd:student>
        
    </xsd:students>
</xsd:class>
原文链接:https://www.f2er.com/xml/296877.html

猜你在找的XML相关文章