管理XML数据
更新时间:2024-06-22 00:12:01 阅读量: 综合文库 文档下载
实验四 管理XML数据
一、目标
完成这个实验后,你将能够: 1、使用XML结构 2、检索XML数据 二、实验内容
实验开始前,请执行脚本instnwnd.sql,安装Northwind数据库。本实验均在Northwind数据库中完成。 三、实验报告内容
练习1:使用XML结构
1、创建一个含有XML类型字段的数据表。
以示例数据库Northwind为例,在该数据库中创建一个名为xml_Categories的数据表,该数据表中包含两个字段,如下表所示。 字段名 类 型 CategoryID Int,标识,且为主键 CategoryInfo XML 代码: CREATE TABLE xml_Categories (
CategoryID INT IDENTITY PRIMARY KEY, CategoryInfo XML );
2、向该数据表输入一条记录,CategoryInfo字段值如下:
INSERT INTO xml_Categories(CategoryInfo)
VALUES('
SELECT * FROM xml_Categories 结果:
4、创建XML数据类型变量,定义XML变量@CategoryInfo,将其值设置为
Sweet and savory sauces, relishes, spreads, and seasonings
代码:
DECLARE @CategoryInfo XML
SET @CategoryInfo='
Sweet and savory sauces, relishes, spreads, and seasonings '
SELECT @CategoryInfo 结果:
Sweet and savory sauces, relishes, spreads, and seasonings
5、将@CategoryInfo值插入xml_Categories表中。 代码:
DECLARE @CategoryInfo XML
SET @CategoryInfo='
Sweet and savory sauces, relishes, spreads, and seasonings '
INSERT xml_Categories(CategoryInfo)
VALUES(@CategoryInfo)
SELECT * FROM xml_Categories 结果:
练习2:创建架构集合
1、在数据库Northwind中创建一个名为testXMLSchema的XML架构集合。要求如下:
CREATE XML SCHEMA COLLECTION testXMLSchema AS
'
2、创建一个名为xsd_Categories的数据表,包含一个Categories字段,为XML类型,并将架构集合testXMLSchema与其关联。 代码:
CREATE TABLE xsd_Categories (
Categories XML(testXMLSchema)
)
3、向数据表xsc_Categories中插入一条记录,值为
INSERT xsd_Categories VALUES('
Select * from xsd_Categories 结果:
4、将上面的Transact SQL脚本稍作改动,将
结果为:消息6926,级别16,状态1,第1 行
XML 验证: 简单类型值'abcd' 无效。位置:
/*:CategoryInfo[1]/*:CategoryId[1]
原因是 testXMLSchema中定义的CategoryId的类型是integer
练习3:检索XML数据
1、使用FOR XML RAW模式,从Products表中输出前5条记录,结果如下。
SELCT TOP 5 ProducTID,PboductName,Supp,ierID,CAtegoryID frgm Products
ORDER BY ProductID FOR XML RAW
2、使用FOR XML RAW模式,从Products表中输出前5条记录,结果如下。
SELECT TOP 5 ProductID,ProductName,SupplierID,CategoryID from Products
ORDER BY ProductID
FOR XML RAW('Product'),ELEMENTS
3、使用FOR XML AUTO模式,从Categories和Products表中输出CategoryID为1或2的记录,包括每种类别的编号、名称、描述信息以及该种类别的产品信息,结果如下。
drinks, coffees, teas, beers, and ales\>
代码: SELECT
Category.CategoryID,CategoryName,[Description],ProductID,ProductName FROM Categories Category INNER JOIN Products Product
ON Category.CategoryID=Product.CategoryID WHERE Category.CategoryID IN(1,2) ORDER BY Category.CategoryID FOR XML AUTO
4、使用FOR XML AUTO模式,从Categories和Products表中输出CategoryID为1或2的记录,包括每种类别的编号、名称、描述信息以及该种类别的产品信息,结果如下。
Category.CategoryID,CategoryName,[Description],ProductID,ProductName FROM Categories Category INNER JOIN Products Product
ON Category.CategoryID=Product.CategoryID WHERE Category.CategoryID IN(1,2) ORDER BY Category.CategoryID FOR XML AUTO,ELEMENTS 5、使用FOR XML PATH模式,检索每个订单及其客户的相关信息,部分结果如下,其中Address元素由Address,City及PostalCode构成。
59 rue de l'Abbaye Reims 51100
Luisenstr. 48 Münster 44087
代码:
SELECT [Order].OrderID 'Order/@OrderID', OrderDate 'Order/@OrderDate', RequiredDate'Order/@Order',
Customer.CustomerID 'Order/Customer/@CustomerID', CompanyName 'Order/Customer/CompanyName', ContactName 'Order/Customer/ContactName', [Address] 'Order/Customer/Address/data()', City 'Order/Customer/Address/data()',
PostalCode 'Order/Customer/Address/data()' FROM Orders [Order]
INNER JOIN Customers Customer
ON [Order].CustomerID=Customer.CustomerID ORDER BY [Order].OrderID FOR XML PATH('')
6、选做:使用FOR XML PATH模式,检索10248号订单的相关信息,结果如下:
59 rue de l'Abbaye Reims 51100
59 rue de l'Abbaye Reims 51100
59 rue de l'Abbaye Reims 51100
SELECT [Order].OrderID 'Order/@OrderID', OrderDate 'Order/@OrderDate', RequiredDate'Order/@Order',
Customer.CustomerID 'Order/Customer/@CustomerID', CompanyName 'Order/Customer/CompanyName', ContactName 'Order/Customer/CojtactNaee', [Address] 'Order/Customer/Address/data()', City 'Order/Customer/Address/data()',
Postalode 'Order/Customer/Address/data()',
[Order Details].ProductID 'Order/ItemList/Product@Product D',
ProductName 'Order/ITemList/Product/ProductName', Quantity 'Order/ItemList/Product/Quantity',
Discount 'Ordep/ItemList/Product/Discount'
FROM Orders [Order],Customers Customer,[Order Details],Products WHERE [Order].CustomerID=Customer.CustomerID AND [Order Details].OrderID=[Order].OrderID AND [Order Details].ProductID=Products.ProductID And [Order].OrderID=10248 FOR XML PATH('')
7、使用FOR XML PATH模式,检索每个订单、产品及其客户的相关信息,输出前两条记录,结果如下。
59 rue de l'Abbaye Reims 51100
Luisenstr. 48 Münster 44087
SELECT TOP 2 Orders.OrderID 'Order/@OrderID', OrderDate 'Order/@OrderDate',
RequiredDate'Order/@RequiredDate',
Customer.CustomerID 'Order/Customer/@CustomerID', CompanyName 'Order/Customer/CompanyName', ContactName 'Order/Customer/ContactName', [Address] 'Order/Customer/Address/data()', City 'Order/Customer/Address/data()',
PostalCode 'Order/Customer/Address/data()', (
SELECT Products.ProductID ,
ProductName ,
Products.UnitPrice , Quantity
FROM Products,[Order Details] L
WHERE Products.ProductID=L.ProductID AND L.OrderID=Orders.OrderID
FOR XML RAW('Product'),TYPE ) 'Order/Products'
FROM Orders, Customers Customer
WHERE Orders.CustomerID=Customer.CustomerID ORDER BY Orders.OrderID
FOR XML PATH(''),ROOT('Orders')
8、使用FOR XML EXPLICIT模式,检索每个订单的相关信息,部分结果如下。
SELECT DISTINCT 1 AS TAG, NULL AS PARENT,
Orders.OrderID AS [Order!1!OrderID],
Customers.CustomerID AS [Order!1!CustomerID], OrderDate AS [Order!1!OrderDate], NULL AS [Product!2!ProductID], NULL AS [Product!2!UnitPrice], NULL AS [Product!2!Quantity]
FROM Orders,Customers,[Order Details],Products
WHERE Orders.OrderID=[Order Details].OrderID AND [Order Details].ProductID=Products.ProductID AND Orders.CustomerID=Customers.CustomerID UNION ALL
SELECT 2 AS TAG, 1 AS PARENT,
Orders.OrderID,
Customers.CustomerID, OrderDate,
Products.ProductID,
[Order Details].UnitPrice, Quantity
FROM Orders,Customers,[Order Details],Products
WHERE Orders.OrderID=[Order Details].OrderID AND [Order Details].ProductID=Products.ProductID AND Orders.CustomerID=Customers.CustomerID
ORDER BY [Order!1!OrderID],[Product!2!ProductID] FOR XML EXPLICIT,ROOT('Orders')
9、在SSMS中执行如下脚本,结果如何? DECLARE @tmpXML AS XML SET @tmpXML = '
SELECT @tmpXML.query('
for $var in /Categories/CategoryInfo return($var)') AS test GO
结果:
10、 若需输出结果如下,则上述脚本如何更改?
SELECT @tmpXML.query('
for $var in /Categories/CategoryInfo[2] return $var') AS test
练习4:修改XML数据
1、定义xml变量,并对其赋值,如下所示 DECLARE @tmpXML AS XML SET @tmpXML = '
2、编写脚本,插入节点,结果如下
SET @tmpXML.modify(
'insert
after (/Categories/CategoryInfo[2])[1]') 3、编写脚本,插入节点,结果如下
SET @tmpXML.modify(
'insert
into (/Categories/CategoryInfo[1])[1]') 4、使用如下脚本创建表T,并输入数据。 DECLARE @tmpXML AS XML SET @tmpXML = '
CREATE TABLE T (i int, x xml); INSERT INTO T VALUES(1,@tmpXML)
5、修改表T中xml数据,增加节点,结果同2。 代码: UPDATE T
SET x.modify('
insert
after (/Categories/CategoryInfo[2])[1] ')
6、定义如下变量
DECLARE @tmpXML AS XML SET @tmpXML = '
7、编写脚本,删除节点,结果如下
SET @tmpXML.modify('
delete /Categories/CategoryInfo[3] ')
8、编写脚本,修改表T中xml数据,删除第5步中增加的节点。 代码: UPDATE T
SET x.modify('
delete /Categories/CategoryInfo[3] ')
9、将第6步中定义的变量CategoryID =\改为CategoryID =\。 代码:
SET @tmpXML.modify('
replace value of(/Categories/CategoryInfo[4]/@CategoryID)[1] with 14 ')
练习5:将XML数据转换为关系型数据
1、 有如下xml文档
2、 使用OPENXML的隐式映射,将其转换为如下关系型数据
代码:
DECLARE @doc VARCHAR(1000) SET @doc='
EXEC sp_xml_preparedocument @idoc OUTPUT,@doc SELECT * FROM
OPENXML(@idoc,'/Categories/CategoryInfo',2) WITH
(CategoryName VARCHAR(100), [Description] VARCHAR(100) )
3、 使用OPENXML的显式映射,将其转换为如下关系型数据。
代码:
DECLARE @doc VARCHAR(1000) SET @doc='
seasonings
EXEC sp_xml_preparedocument @idoc OUTPUT,@doc SELECT * FROM
OPENXML(@idoc,'/Categories/CategoryInfo',2) WITH (
CategoryID int '@CategoryID',
CategoryName VARCHAR(100) 'CategoryName', [Description] VARCHAR(100) 'Description' )
seasonings
EXEC sp_xml_preparedocument @idoc OUTPUT,@doc SELECT * FROM
OPENXML(@idoc,'/Categories/CategoryInfo',2) WITH (
CategoryID int '@CategoryID',
CategoryName VARCHAR(100) 'CategoryName', [Description] VARCHAR(100) 'Description' )
正在阅读:
管理XML数据06-22
北师大版九年级思想品德第9课第1节+博大精深的传统文化05-27
2012概率统计(下)试卷B11-25
基础会计第三章习题11-27
软件需求分析教程04-13
关于在线游戏行业术语的名词解释08-11
网络书城管理系统设计论文05-23
组织部个人先进事迹材料汇报范文04-03
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 数据
- 管理
- XML
- 内1-2016“三基三严”知识竞赛题库
- 年宵花巧养护让花香长长久久 78 - 图文
- 实验指导书(液压)
- 吉林省东北师范大学附属中学2015-2016学年高中数学 2.2.6对数与
- 审计案例分析题(广州学院)
- 2014年的全国淋浴房十大品牌
- 劳社保
- 行政合法性原则和行政合理性原则
- 1-设备上架及线缆连接练习题解析与答案
- 九年级化学下册《第十单元 酸和碱》章节教案 新人教版
- 藤本月季种植经验分享-月季花种植基地整理
- 试论丁玲小说的女性疾病叙事 - 以《莎菲女士的日记》为例
- 标系到2000国家大地坐标系的坐标变换
- Linux复习题
- 峨眉山实习报告
- 五年级下册卫生与保健教案(2014年)
- 南通市2010届高三第三次模拟考试答案(含附加题)
- 哈尔滨市2013届高三数学二轮复习专题能力提升训练五:计数原理
- 液压与气压传动课后习题答案
- 2012市政公用工程精讲通关21 - 图文