SQL Server操作XML(四)XML数据类型
(1)定义
--表中定义
CREATE TABLE [dbo].[Invoice](
[Invoice_ID] [int] NULL,
[SalesDate] [date] NULL,
[ItemList] [xml] NULL
)
--程序中定义
[Invoice_ID] [int] NULL,
[SalesDate] [date] NULL,
[ItemList] [xml] NULL
)
--程序中定义
declare @xdoc xml;
--隐式类型转换
SET @xdoc =’
<Customer CustomerID="VINET" ContactName="Paul Henriot">
<Order CustomerID="VINET" EmployeeID="5" OrderDate="1996-07-04T00:00:00">
<OrderDetail OrderID="10248" ProductID="11" Quantity="12"/>
</Order>
</Customer>‘
<Order CustomerID="VINET" EmployeeID="5" OrderDate="1996-07-04T00:00:00">
<OrderDetail OrderID="10248" ProductID="11" Quantity="12"/>
</Order>
</Customer>‘
--显式类型转换
SET @xdoc=CAST('') AS XML 或CONVERT(XML,'')
--Untype XML
CREATE TABLE Orders
(OrderID int IDENTITY(1,1),
CustomerID int,
OrderDetails xml)
--可以插入任意字符串到Untype XML字段中
INSERT Orders VALUES('1','<Product ID="1" QTY="100"/><Product ID="2" QTY="100"/>')
(OrderID int IDENTITY(1,1),
CustomerID int,
OrderDetails xml)
--可以插入任意字符串到Untype XML字段中
INSERT Orders VALUES('1','<Product ID="1" QTY="100"/><Product ID="2" QTY="100"/>')
--Type XML
--声明XML 的Schema 结构,用于Type验证
--通常利用 FOR XML schema自动生成
CREATE XML SCHEMA COLLECTION SalesSchema
AS
'<?xml version="1.0" standalone="yes"?>
<xs:schema id="Sales" xmlns="http://www.gocean.com.cn" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Sales" msdata:IsDataSet="true" msdata:Locale="zh-CN">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Product">
<xs:complexType>
<xs:sequence>
<xs:element name="ID" type="xs:int" minOccurs="0" />
<xs:element name="Name" type="xs:string" minOccurs="0" />
<xs:element name="Qty" type="xs:int" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
--声明Type类型的XML 字段AS
'<?xml version="1.0" standalone="yes"?>
<xs:schema id="Sales" xmlns="http://www.gocean.com.cn" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Sales" msdata:IsDataSet="true" msdata:Locale="zh-CN">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Product">
<xs:complexType>
<xs:sequence>
<xs:element name="ID" type="xs:int" minOccurs="0" />
<xs:element name="Name" type="xs:string" minOccurs="0" />
<xs:element name="Qty" type="xs:int" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
CREATE TABLE Orders
(OrderID int IDENTITY(1,1),
CustomerID int,
OrderDetail xml (SalesSchema))
(OrderID int IDENTITY(1,1),
CustomerID int,
OrderDetail xml (SalesSchema))
--符合Schema中Type规范
insert orders values(1,'<Sales><Product><ID>1</ID><Name>p1</Name><Qty>100</Qty></Product></Sales>')
--不符合Schema中Type规范
insert orders values(1,'<Salesman><Product><ID>1</ID><Name>p1</Name><Qty>100</Qty></Product></Salesman>')
显式错误:XML 验证: 找不到元素 'Salesman' 的声明。位置: /*:Salesman[1]
CREATE TABLE Orders
(OrderID int IDENTITY(1,1),
CustomerID int,
OrderDetail xml (Content SalesSchema))
--符合Content条件
(OrderID int IDENTITY(1,1),
CustomerID int,
OrderDetail xml (Content SalesSchema))
insert orders values(1,'<Sales><Product><ID>1</ID><Name>p1</Name><Qty>100</Qty></Product></Sales>
<Sales><Product><ID>2</ID><Name>p2</Name><Qty>200</Qty></Product></Sales>')
<Sales><Product><ID>2</ID><Name>p2</Name><Qty>200</Qty></Product></Sales>')
--声明Type类型的XML 字段,必须存入片段,即不仅需要Type进行验证,而且校验是否是标准文档结构
CREATE TABLE Orders
(OrderID int IDENTITY(1,1),
CustomerID int,
OrderDetail xml (DOCUMENT SalesSchema))
--不符合Document结构(OrderID int IDENTITY(1,1),
CustomerID int,
OrderDetail xml (DOCUMENT SalesSchema))
insert orders values(1,'<Sales><Product><ID>1</ID><Name>p1</Name><Qty>100</Qty></Product></Sales>
<Sales><Product><ID>2</ID><Name>p2</Name><Qty>200</Qty></Product></Sales>')
显式错误:XML 验证: XML 实例必须为文档。
<Sales><Product><ID>2</ID><Name>p2</Name><Qty>200</Qty></Product></Sales>')
--符合Document结构
insert orders values(1,'<Sales><Product><ID>1</ID><Name>p1</Name><Qty>100</Qty></Product>
<Product><ID>2</ID><Name>p2</Name><Qty>200</Qty></Product></Sales>')
<Product><ID>2</ID><Name>p2</Name><Qty>200</Qty></Product></Sales>')