xml简明教程第6课 使用xml schema

作者:未知 来源:未知 添加时间:2006年7月3日 字体:

什么是xml schema? 
xml schema是一个基于xml的语法或schema规范,用来定义xml文档的标记方式。xml sc 
hema是一个由microsoft建议的schema规范,它与文档类型定义(dtd)相比具有很大的 
优势,而dtd是最初用来定义xml模型的schema规范。dtd存在很多缺点,包括使用非xml 
语法,不支持数据类型定义,不具有扩展性等。例如,dtd不允许把元素内容定义为另外 
一个元素,或字符串。想了解更多有关dtd的内容,可以参考w3c xml建议书。 xml sch 
ema从几个方面改善了dtd,包括使用xml语法,支持数据类型定义和名域。例如,xml s 
chema允许你把一个元素说明为一个integer, float,boolean, url等类型。 
在internet explorer 5中的xml解析器可以解析使用dtd或xml schema的xml文档。 
如何建立xml schema? 
请在下面的xml文档中找一找每个节点的schema声明。 
<class xmlns="x-schema:classschema.xml"> 
      <student studentid="13429"> 
         <name>jane smith</name> 
         <gpa>3.8</gpa> 
     </student> 
</class> 
你会注意到在上面文档中默认的名域是“x-schema:classschema.xml”。这告诉解析器 
根据url(“classschema.xml”)上的schema(x-schema)来解析整个文档。 
下面是上面那个文档的完整的schema。注意schema的根元素中的名域声明。第一个(xml 
ns=”urn:schemas-microsoft-com:xml-data”)表明这个xml文档是一个xml schema。第 
二个(xmlns:dt=”urn:schemas-microsoft-com:datatypes”)允许schema处理者在elem 
enttype和attributetype声明中的type属性前加dt前缀来说明元素的类型和内容的特征 
。 
<schema  xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-mi 
crosoft-com:datatypes"> 
     <attributetype name='studentid' dt:type='string' required='yes'/> 
     <elementtype name='name' content='textonly'> 
     <elementtype name='gpa' content='textonly' dt:type='float'/> 
     <elementtype name='student' content='mixed'> 
         <attribute type='studentid'/> 
         <element type='name'/> 
         <element type='gpa'/> 
     </elementtype> 
     <elementtype name='class' content='eltonly'> 
         <element type='student'/> 
     </elementtype> 
</schema> 
用来定义元素和属性的声明元素如下所示: 
元    素 
 说    明 
elementtype 把类型和条件赋给一个元素,可以包含子元素 
attributetype 把类型和条件赋给一个属性 
attribute 声明以前定义过的属性类型可以出现在名为elementtype的元素的作用域中 
element 声明以前定义过的元素类型可以出现在名为elementtype的元素的作用域中 
  
schema的内容以最深层的元素的“attributetype”和“elementtype”声明开头。 
<attributetype name='studentid' dt:type='string' required='yes'/> 
<elementtype name='name' content='textonly'> 
<elementtype name='gpa' content='textonly' dt:type='float'/> 
elementtype声明后接着就是它的属性和子元素,如果一个元素有属性或子元素,必须把 
这种方法包含在它的elementtype声明中,或者是必须在它们自己的 elementtype 或 a 
ttributetype声明中进行前导声明。 
<elementtype name='student' content='mixed'> 
     <attribute type='studentid'/> 
     <element type='name'/> 
     <element type='gpa'/> 
</elementtype> 
不同于dtds,xml schema允许有一个开放的内容模式,你可以进行定义数据类型、使用 
默认值等等操作而不必限定内容。 
在下面的schema中,<gpa> 元素的类型被定义并有一个默认值,但在 <student> 元素中 
没有其他节点被声明。 
<schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-mic 
rosoft-com:datatypes"> 
      <attributetype name="scale" default="4.0"/> 
      <elementtype name="gpa" content="textonly" dt:type="float"> 
         <attribute type="scale"/> 
      </elementtype> 
      <attributetype name="studentid"/> 
      <elementtype name="student" content="eltonly" model="open" order="many 
"> 
         <attribute type="studentid"/> 
         <element type="gpa"/> 
      </elementtype> 
</schema> 
上面的schema允许你只验证你所关心的区域,这使你对文档的验证级别有更多的控制, 
并允许你使用schema提供的一些特性而不必严格确认。 
尝试一下! 
试着处理一下下面的xml文档的schema。 
<order> 
  <customer> 
    <name>fidelma mcginn</name> 
    <phone_number>425-655-3393</phone_number> 
  </customer> 
  <item> 
    <number>5523918</number> 
    <description>shovel</description> 
    <price>39.99<price> 
  </item> 
  <date_of_purchase>1998-10-23</date_of_purchase> 
  <date_of_delivery>1998-11-03</date_of_delivery> 
</order> 
如果你处理完schema,在xml validator中运行 . 
下载msdn online的区域中提供了一套 xml sample files, 包括一个有相关schema的xm 
l文档。下载这些示例程序可以与xml文档和schema一起运行。如果你想要根据一个sche 
ma来测试xml的有效性,可以从xml validator 处下载文档或查看mime type viewer中的 
xml文件。 
一些说明: 
elementtype和 attributetype 声明必须放在attribute和element内容声明之前。例如 
,在上面的schema中,<gpa>元素的elementtype声明必须放在<student>元素的element  

type声明之前。  
order属性的默认值是建立在content属性的值上的。当content值为“eltonly”时,or 
der默认值是seq。当content值为“mixed”时,order默认值是“many”。see 如果想了 
解有关这些缺省值的信息,可以查看 xml schema reference。

ppdesk