Jinghao Liu (刘劲浩)'s BLOG

一个专门有关SQL Server的中文博客. 主要面向中国的SQL Server用户. 侧重于介绍的SQL Server 2005在XML方面的功能.也会尽力解答其他SQL相关问题.

SQL2005对XSD的支持更丰富了数据建模的方法

在SQL2005中,有一个新的顶级对象(top level object)被加入: XML架构集合(XML Schema Collection).  它提供给用户贮存XSD (XML Schema Definition)并用之验证特定XML格式的能力. 更重要的是,它提供了一种可更丰富描述数据模型的方法

举例:
create xml schema collection xsdPurchaseOrder as
N'<schema targetNamespace="http://www.example.com/IPO" xmlns="http://www.w3.org/2001/XMLSchema"
 xmlns:ipo="http://www.example.com/IPO" elementFormDefault="qualified">

 <annotation>
   <documentation xml:lang="zh-Hans">
  为国际订货单据定义的各种地址格式
  Copyright 2000 Example.com. All rights reserved.
   </documentation>
 </annotation>

 <!-- 一般地址 -->
 <complexType name="Address">
   <sequence>
     <element name="name" type="string"/>
     <element name="street" type="string"/>
     <element name="city" type="string"/>
   </sequence>
 </complexType>

 <!-- 美国地址附加信息 -->
 <complexType name="USAddress">
   <complexContent>
     <extension base="ipo:Address">
       <sequence>
         <element name="state" type="ipo:USState"/>
         <element name="zip" type="positiveInteger"/>
       </sequence>
     </extension>
   </complexContent>
 </complexType>

 <!-- 中国地址附加信息 -->
 <complexType name="CNAddress">
   <complexContent>
     <extension base="ipo:Address">
       <sequence>
         <element name="省" type="ipo:CNProvince"/>
       <element name="邮编" type="positiveInteger"/>
    </sequence>
  </extension>
   </complexContent>
 </complexType>

 <!-- 定义美国州的数据类型 -->
 <simpleType name="USState">
   <restriction base="string">
     <enumeration value="AK"/>
     <enumeration value="AL"/>
     <enumeration value="AR"/>
     <!-- and so on ... -->
     <enumeration value="PA"/>
   </restriction>
 </simpleType>

 <!-- 定义中国省的数据类型 -->
 <simpleType name="CNProvince">
   <restriction base="string">
     <enumeration value="湖南"/>
     <enumeration value="湖北"/>
     <enumeration value="辽宁"/>
     <!-- and so on ... -->
     <enumeration value="江苏"/>
   </restriction>
 </simpleType>

 <!-- 根元素 -->
 <element name="purchaseOrder" type="ipo:PurchaseOrderType"/>

 <element name="comment" type="string"/>

 <complexType name="PurchaseOrderType">
  <sequence>
   <element name="shipTo" type="ipo:Address"/>
   <element name="billTo" type="ipo:Address"/>
   <element ref="ipo:comment" minOccurs="0"/>
   <!-- 为了简化我剩去了items的复杂定义 -->
   <element name="items" type="string"/>
  </sequence>
  <attribute name="orderDate" type="date"/>
 </complexType>
</schema>'
go

针对上述格式,我们可将下面的订单存入数据库中带有此架构集合的XML列中. SQL Server将对要存入的XML进行验证(validate),确保数据的格式. 这实际上就提供了新的数据建模方法.

create table tbl_Orders( id int, PurchaseOrder xml(xsdPurchaseOrder) )
insert tbl_Orders value( 1,
N'<purchaseOrder xmlns="http://www.example.com/IPO" orderDate="2005-10-30">
 <shipTo>
  <name>兴业公司</name>
   <street>兴业路十五号</street>
   <city>无锡</city>
   <省>江苏</省>
   <邮编>12345</邮编>
 </shipTo>
 <billTo>
  <name>ABC Corp</name>
  <street>1710 Madison Ave.</street>
  <city>Jamesville</city>
  <state>PA</state>
  <zip>98000</zip>
 </billTo>
 <comment>ABC Corp 购买一批组件并直接寄送兴业公司组装</comment>
 <items>541045系列组件</items>
</purchaseOrder>' )
go

在上述例子中,我们定义了不同格式的地址并用xs:enumeration为<省>和<state>约束了数值范围.

Published Sunday, March 12, 2006 10:03 PM by jinghaol

Comments

 

SteveGY_CN said:

i searched XSD in google and find this
i realy like the xml data type and the recursive query CTE in SQL server 9.0
May 6, 2006 5:17 AM
 

qqtt said:

呵呵~!
May 15, 2006 8:58 PM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker