技术开发 频道

SQL Server 2005如何处理XML格式详解

  B. 针对 xml 类型的列指定 nodes() 方法

  在此示例中使用自行车生产说明,并将其存储在 ProductModel 表的 Instructions xml 类型列中。有关详细信息,请参阅 AdventureWorks 数据库中的 xml 数据类型表示形式。

  在以下示例中,nodes() 方法是针对 ProductModel 表中 xml 类型的 Instructions 列指定的。

  nodes() 方法通过指定 /MI:root/MI:Location 路径将 元素设置为上下文节点。结果行集包括原始文档的逻辑副本,每个副本对应文档中的一个 节点,上下文节点设置为 元素。因此,nodes() 函数给出一组 上下文节点。

  query() 方法针对此行集请求 self::node,因此将返回每行中的 <Location> 元素。

  在此示例中,查询在特定产品样式的生产说明文档中将每一个 <Location> 元素都设置为上下文节点。您可以使用这些上下文节点来按照以下方式来检索值:

  在每个 <Location>中查找 LocationID

  在每个 <Location>中检索生产步骤( <step>子元素)

  此查询使用 query() 方法返回上下文项,其中指定了 self::node() 的缩写语法 "."。

  请注意以下方面:

  nodes() 方法应用于 Instructions 列并返回一个行集 T (C)。此行集包含将 /root/Location 作为上下文项的原始生产说明文档的逻辑副本。

  CROSS APPLY 将 nodes() 应用于 Instructions 表中的每一行,并只返回构成结果集的行。

SELECT C.query('.') as result
FROM Production.ProductModel
CROSS APPLY Instructions.nodes(
'
declare namespace MI="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
/MI:root/MI:Location') as T(C)
WHERE ProductModelID=7

 

  下面是部分结果:

<MI:Location LocationID="10"  ...>
  
<MI:step ... />
      ...
</MI:Location>
<MI:Location LocationID="20"  ... >
    
<MI:step ... />
      ...
</MI:Location>
...

 

  以下查询与先前的查询相似,但是它通过使用行集中的上下文节点,利用 value() 和 query() 检索一组值。对于每个位置,SELECT 子句都检索在该位置使用的位置 ID 和工具。

SELECT C.value('@LocationID','int') as LId,
       C.query('declare namespace MI="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
                 MI:step/MI:tool') as result
FROM    Production.ProductModel
CROSS APPLY Instructions.nodes(
'
declare namespace MI="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
/MI:root/MI:Location') as T(C)
WHERE ProductModelID=7

 

  结果如下。为提高可读性,未显示命名空间。

LId  result
10  <MI:tool xmlns:MI="...">T-85A framing tool</MI:tool>
    
<MI:tool xmlns:MI="...">Trim Jig TJ-26</MI:tool>
    
<MI:tool xmlns:MI="...">router with a carbide tip 15</MI:tool>
      
<MI:tool xmlns:MI="...">Forming Tool FT-15</MI:tool>
20
30  <MI:tool xmlns:MI="...">standard debur tool</MI:tool>
45  <MI:tool xmlns:MI="...">paint harness</MI:tool>
50
60

 

  C. 将 nodes() 应用于由其他 nodes() 方法返回的行集

  以下代码查询 ProductModel 表的 Instructions 列中生产说明的 XML 文档。此查询返回包含产品样式 ID、生产位置和生产步骤的行集。

  请注意以下方面:

  首先,nodes() 方法应用于 Instructions 列并返回 T1(位置)行集。此行集包含将 /root/Location 作为上下文项的原始生产说明文档的逻辑副本。

  其次,nodes() 应用于 T1(位置)行集并返回 T2(步骤)行集。此行集包含将 /root/Location 作为上下文项的原始生产说明文档的逻辑副本。

SELECT ProductModelID, Locations.value('./@LocationID','int') as LocID,
steps.query('.') as Step      
FROM Production.ProductModel      
CROSS APPLY Instructions.nodes(
'      
declare namespace MI="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";      
/MI:root/MI:Location') as T1(Locations)      
CROSS APPLY T1.Locations.nodes('      
declare namespace MI="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";      
.
/MI:step ') as T2(steps)      
WHERE ProductModelID=7      
go      
-- result      
ProductModelID LocID Step      
----------------------------      
7      10   <step ... />      
7      10   <step ... />      
...      
7      20   <step ... />      
7      20   <step ... />      
7      20   <step ... />      
...      

 

  此查询两次声明 MI 前缀。此外,您可以使用 WITH XMLNAMESPACES 来声明一次前缀并在查询中使用它:

WITH XMLNAMESPACES (
  
'http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions'  AS MI)

SELECT ProductModelID, Locations.value('./@LocationID','int') as LocID,
steps.query('.') as Step      
FROM Production.ProductModel      
CROSS APPLY Instructions.nodes(
'      
/MI:root/MI:Location') as T1(Locations)      
CROSS APPLY T1.Locations.nodes('      
./MI:step ') as T2(steps)      
WHERE ProductModelID=7      
go  

 

  以下查询与先前的查询相似,但是它将 exist() 方法应用于 T2(步骤)行集中的 XML,以便只检索至少使用一个生产工具的生产步骤。即: 元素至少包含一个 子元素。

WITH XMLNAMESPACES (
  
'http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions' AS MI)

SELECT ProductModelID,
       Locations.value(
'./@LocationID','int') as LocID,
       steps.query('.') as Steps
FROM   Production.ProductModel
CROSS APPLY Instructions.nodes(
'/MI:root/MI:Location') as T1(Locations)
CROSS APPLY T1.Locations.nodes('./MI:step') as T2(steps)
WHERE  ProductModelID=7
AND    steps.exist('./MI:tool') = 1
Go

       以上是一点小小的分享,希望对大家的sql进步有帮助。

0
相关文章