XML之Schema

        前面学习了DTD,同样我们有了一套更完善的定义法则-Schema。下面围绕Schema是什么,为何用以及怎么用谈谈自己的感受。      

        XML Schema是基于XML的DTD替代者。

        XML Schema可描述XML文档的结构。

        XML Schema语言可作为XSD(XML Schema Definition)来引用。

       

       是什么

      XML Schema的作用是定义XML文档的合法构建模块,类似DTD。

        XML Schema是DTD的继任者。

        XML是W3C标准。

        为何用

         我们已经有了DTD,为何还要用XML Schema,当然它有自身的优越性。

         XML Schema比DTD更强大:

         XML Schema支持数据类型

         XML Schema使用XML语法

         XML Schema可保护数据通信

         XML Schema可扩展

         形式良好的XML文档是不够的,通过XML Schema,大部分这样的错误会被您的验证软件捕获到。

         怎么

      XML文档可对DTD或XML Schema进行引用。

         一个简单的XML文档:

        请看这个名为"note.xml"的XML文档:

<span style="font-size:18px;"><?xml version="1.0"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note></span>
            DTD文件

       下面这个例子是名为"note.dtd"的DTD文件,它对上面那个XML文档的元素进行了定义:

<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from(#PCDATA)>
<!ELEMENT heading(#PCDATA)>
<!ELEMENT body(#PCDATA)>
        XML Schema
       下面这个例子是一个名为“note.xsd”的XML Schema文件,它定义了上面那个XML文档的元素:

<span style="font-size:18px;"><?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
targetNamespace="httpk://www.w3school.com.cn"
xmlns="http://www.w3school.com.cn"
elementFormDefault ="qualified">

<xs:element name="note">
     <xs:complexType>
            <xs:sequence>
                  <xs:element name="to" type="xs:string"/>
                  <xs:element name="from" type="xs:string"/>
                  <xs:element name="heading" type="xs:string"/>
                  <xs:element name="body" type="xs:string"/>
            </xs:sequence>
     </xs:complexType>
</xs:element>

</xs:schema></span>
        note元素是一个复合类型,因为它包含其他的子元素。其他元素(to,from,heading,body)是简易类型,因为它们没有包含其他元素。

    对DTD的引用

    此文件包含对DTD的引用:

<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "http://www.w3school.com.cn/dtd/note.dtd">
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
    对XML Schema的引用

    此文件包含对XML Schema的引用

<?xml version="1.0"?>
<note
xmlns="http://www.w3school.com.cn"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3school.com.cn note.xsd">

<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
      小结

     Schema的知识不止这些,还学要深入地了解和学习。学习了如何使用XML Schema来定义XML文档的合法元素,就像DTD。我们应该认识到,XML Schema作为DTD的替代者,会很快被应用于大多数Web应用程序中。 

       

       

         

        

      

     

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 21
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值