以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DTD/XML Schema 』  (http://bbs.xml.org.cn/list.asp?boardid=23)
----  对一个xml编写xsd的实例[原创]  (http://bbs.xml.org.cn/dispbbs.asp?boardid=23&rootid=&id=53306)


--  作者:hongjuesir
--  发布时间:9/30/2007 4:07:00 PM

--  对一个xml编写xsd的实例[原创]
有坛中兄弟给我发了一个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>


[此贴子已经被作者于2007-9-30 16:54:38编辑过]

--  作者:roverhuang
--  发布时间:10/14/2007 1:36:00 AM

--  
这样写没有输出结果呀,最好用个样式表(XSL)把XML文件显示出来,看看是否XSD对XML文件中的记录元素类型有约束,
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
5,203.125ms