对一个xml编写xsd的实例

来源:互联网 发布:外壳设计软件 编辑:程序博客网 时间:2024/04/27 22:35

有坛中兄弟给我发了一个xml,须写xsd验证,这方面我也是新手呢,不过看了http://www.w3school.com.cn/schema/index.asp的教程而已,所以这倒是我第一次写xsd了。

给定xml (project_4.xml)文件如下:

<UWO>
    
<Departments>
  
<Department>
   
<Name>Economics</Name>
   
<Address>Social Science Centre, UWO, London, Ontario, Canada , N6A 5C2</Address>
   
<Location>SSC 4071</Location>
   
<Phone>519 661-3500 x83500</Phone>
   
<Facsimile>519 661-3666 x83666</Facsimile>
   
<Email>economics@uwo.ca</Email>
   
<WEB>http://economics.uwo.ca</WEB>
  
</Department>
  
<Department>
   
<Name>Political_science</Name>
   
<Address>Social Science Centre, UWO, London, Ontario, Canada , N6A 5C2</Address>
   
<Location>SSC 4154</Location>
   
<Phone>519 661-3266 x83266</Phone>
   
<Facsimile>519 661-3904 x83904</Facsimile>
   
<Email>polisci-web@uwo.ca</Email>
   
<WEB>http://politicalscience.uwo.ca</WEB>
   
<SERVICES>
    
<Service>
     
<Name>Local_Government_program</Name>
     
<Phone>519 661-2111 x80501</Phone>
     
<Location>SSC 4148</Location>
     
<WEB>http://localgovernment.uwo.ca</WEB>
    
</Service>
    
<Service>
     
<Name>Politics_020E_course_Coordinator</Name>
     
<Phone>519 661-2111 x85108</Phone>
     
<Location>ssc 4149</Location>
    
</Service>
   
</SERVICES>
  
</Department>
   
</Departments>
</UWO>

 编写xsd文件(project.xsd):

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://bbs.xml.org.cn" xmlns="http://bbs.xml.org.cn" elementFormDefault="qualified">
    
<xsd:element name="UWO">
        
<xsd:complexType>
            
<xsd:sequence>
                
<xsd:element name="Departments">
                    
<xsd:complexType>
                        
<xsd:sequence>
                            
<xsd:element name="Department" type="departmentType" maxOccurs="unbounded"/><!--调用后面定义的departmentType类型-->
                        
</xsd:sequence>
                    
</xsd:complexType>
                
</xsd:element>
            
</xsd:sequence>
        
</xsd:complexType>
    
</xsd:element>
    
<xsd:group name="departmentBasic"><!--定义一个group-->
        
<xsd:sequence>
            
<xsd:element name="Name" type="xsd:string"/>
            
<xsd:element name="Address" type="xsd:string"/>
            
<xsd:element name="Location" type="xsd:string"/>
            
<xsd:element name="Phone" type="xsd:string"/>
            
<xsd:element name="Facsimile" type="xsd:string"/>
            
<xsd:element name="Email" type="xsd:string"/>
            
<xsd:element name="WEB" type="xsd:string"/>
        
</xsd:sequence>
    
</xsd:group>
    
<xsd:complexType name="departmentType"><!--定义departmentType类型-->
        
<xsd:sequence>
            
<xsd:group ref="departmentBasic"/><!--引用前面定义的group-->
            
<xsd:element name="SERVICES" maxOccurs="1" minOccurs="0" type="servicesType"/><!--调用后面定义的servicesType类型-->
        
</xsd:sequence>
    
</xsd:complexType>
    
<xsd:complexType name="servicesType"><!--定义servicesType类型-->
        
<xsd:sequence>
            
<xsd:element name="Service" maxOccurs="unbounded">
                
<xsd:complexType>
                    
<xsd:sequence>
                        
<xsd:element name="Name" type="xsd:string"/>
                        
<xsd:element name="Phone" type="xsd:string"/>
                        
<xsd:element name="Location" type="xsd:string"/>
                        
<xsd:element name="WEB" type="xsd:string" minOccurs="0"/>
                    
</xsd:sequence>
                
</xsd:complexType>
            
</xsd:element>
        
</xsd:sequence>
    
</xsd:complexType>
</xsd:schema>

当然还要对xml文件进行关联,所以修改后的project_4.xml为:

<?xml version="1.0"?>
<UWO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://bbs.xml.org.cn project.xsd" xmlns="http://bbs.xml.org.cn">
    
<Departments>
  
<Department>
   
<Name>Economics</Name>
   
<Address>Social Science Centre, UWO, London, Ontario, Canada , N6A 5C2</Address>
   
<Location>SSC 4071</Location>
   
<Phone>519 661-3500 x83500</Phone>
   
<Facsimile>519 661-3666 x83666</Facsimile>
   
<Email>economics@uwo.ca</Email>
   
<WEB>http://economics.uwo.ca</WEB>
  
</Department>
  
<Department>
   
<Name>Political_science</Name>
   
<Address>Social Science Centre, UWO, London, Ontario, Canada , N6A 5C2</Address>
   
<Location>SSC 4154</Location>
   
<Phone>519 661-3266 x83266</Phone>
   
<Facsimile>519 661-3904 x83904</Facsimile>
   
<Email>polisci-web@uwo.ca</Email>
   
<WEB>http://politicalscience.uwo.ca</WEB>
   
<SERVICES>
    
<Service>
     
<Name>Local_Government_program</Name>
     
<Phone>519 661-2111 x80501</Phone>
     
<Location>SSC 4148</Location>
     
<WEB>http://localgovernment.uwo.ca</WEB>
    
</Service>
    
<Service>
     
<Name>Politics_020E_course_Coordinator</Name>
     
<Phone>519 661-2111 x85108</Phone>
     
<Location>ssc 4149</Location>
    
</Service>
   
</SERVICES>
  
</Department>
   
</Departments>
</UWO>

原创粉丝点击