新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> DTD, XML Schema(XMLS), RELAX NG
    [返回] 中文XML论坛 - 专业的XML技术讨论区XML.ORG.CN讨论区 - XML技术『 DTD/XML Schema 』 → 对一个xml编写xsd的实例[原创] 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 5224 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 对一个xml编写xsd的实例[原创] 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     hongjuesir 帅哥哟,离线,有人找我吗?魔羯座1982-1-1
      
      
      等级:大三(要不要学学XML呢?)
      文章:73
      积分:625
      门派:XML.ORG.CN
      注册:2007/6/12

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给hongjuesir发送一个短消息 把hongjuesir加入好友 查看hongjuesir的个人资料 搜索hongjuesir在『 DTD/XML Schema 』的所有贴子 点击这里发送电邮给hongjuesir 访问hongjuesir的主页 引用回复这个贴子 回复这个贴子 查看hongjuesir的博客楼主
    发贴心情 对一个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编辑过]

       收藏   分享  
    顶(0)
      




    ----------------------------------------------
    踏实啃书

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/9/30 16:07:00
     
     roverhuang 帅哥哟,离线,有人找我吗?
      
      
      等级:大一(猛啃高等数学)
      文章:15
      积分:109
      门派:XML.ORG.CN
      注册:2007/7/27

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给roverhuang发送一个短消息 把roverhuang加入好友 查看roverhuang的个人资料 搜索roverhuang在『 DTD/XML Schema 』的所有贴子 引用回复这个贴子 回复这个贴子 查看roverhuang的博客2
    发贴心情 
    这样写没有输出结果呀,最好用个样式表(XSL)把XML文件显示出来,看看是否XSD对XML文件中的记录元素类型有约束,
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/10/14 1:36:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 DTD/XML Schema 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/12/22 7:25:15

    本主题贴数2,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    93.750ms