指定路徑運算式步驟中的座標軸

路徑運算式中的軸步包含下列部份:

如需詳細資訊,請參閱<路徑運算式 (XQuery)>。

在 SQL Server 2005 中的 XQuery 實作支援下列軸步。

座標軸 描述

child

傳回內容節點的子系。

descendant (下階)

傳回內容節點的所有下階。

parent

傳回內容節點的父系。

attribute

傳回內容節點的屬性。

self

傳回內容節點本身。

descendant-or-self

傳回內容節點及內容節點的所有下階。

所有的這些軸都是順向軸,除了 parent 軸之外。parent 軸是反向軸,因為它會向後搜尋文件階層。例如,相對路徑運算式 child::ProductDescription/child::Summary 有兩個步驟,而且每個步驟都指定 child 軸。第一步會擷取內容節點的 <ProductDescription> 元素子系。對於每個 <ProductDescription> 元素節點,第二步會擷取 <Summary> 元素節點子系。

相對路徑運算式 child::root/child::Location/attribute::LocationID 有三步。前兩步每個都會指定 child 軸,第三步會指定 attribute 軸。當在 Production.ProductModel 資料表中針對製造指示 XML 文件執行時,運算式會傳回 <root> 元素的 <Location> 元素節點子系之 LocationID 屬性。

範例

本主題中的查詢範例是針對 AdventureWorks 資料庫中的 xml 類型資料行所指定。如需有關這些資料行的詳細資訊,請參閱<在 AdventureWorks 資料庫中的 xml 資料類型表示法>。

A. 指定子軸

下列查詢是針對特定產品型號,從 Production.ProductModel 資料表所儲存的產品目錄描述來擷取 <ProductDescription> 元素的 <Features> 元素節點子系。

SELECT CatalogDescription.query('
declare namespace PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
  /child::PD:ProductDescription/child::PD:Features')
FROM Production.ProductModel
WHERE ProductModelID=19

注意下列項目是從上一個查詢而來:

  • xml 資料類型的 query() 方法可指定路徑運算式。
  • 在路徑運算式中的步驟可指定 child 軸和節點名稱、ProductDescriptionFeatures 做為節點測試。如需節點測試的詳細資訊,請參閱<指定路徑運算式步驟中的節點測試>。

B. 指定 descendant 與 descendant-or-self 軸

下列使用 descendant 與 descendant-or-self 軸本範例中所指定的查詢是針對 xml 類型變數所指定。XML 執行個體已加以簡化,以便於能輕易地說明產生結果中的差異。

declare @x xml
set @x='
<a>
 <b>text1
   <c>text2
     <d>text3</d>
   </c>
 </b>
</a>'
declare @y xml
set @y = @x.query('
  /child::a/child::b
')
select @y

在下列結果中,運算式會傳回 <a> 元素節點的 <b> 元素節點子系:

<b>text1
   <c>text2
     <d>text3</d>
   </c>
</b>

在此運算式中,如果您為下列路徑運算式指定 descendant 軸:

/child::a/child::b/descendant::*,表示您要求 <b> 元素節點的所有下階。

在節點測試中的星號 (*) 代表做為節點測試的節點名稱。因此,descendant 軸、元素節點的主要節點類型將決定要傳回的節點類型。也就是,運算式會傳回所有的元素節點。但不會傳回文字節點。如需主要節點類型以及它與節點測試之關係的詳細資訊,請參閱 指定路徑運算式步驟中的節點測試 主題。

如下列結果所示,將會傳回 <c> 與 <d> 元素節點:

<c>text2
     <d>text3</d>
</c>
<d>text3</d>

如果您指定 descendant-or-self 軸而不是 descendant 軸,/child::a/child::b/descendant-or-self::* 會傳回內容節點、元素 <b> 以及其下階。

以下是結果:

<b>text1
   <c>text2
     <d>text3</d>
   </c>
</b>

<c>text2
     <d>text3</d>
</c>

<d>text3</d> 

下列針對 AdventureWorks 資料庫的範例查詢,將擷取 <ProductDescription> 元素中 <Features> 元素子系的所有下階元素節點:

SELECT CatalogDescription.query('
declare namespace PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
  /child::PD:ProductDescription/child::PD:Features/descendant::*
')
FROM  Production.ProductModel
WHERE ProductModelID=19

C. 指定父軸

下列查詢會傳回 Production.ProductModel 資料表中所儲存的產品目錄 XML 文件的 <ProductDescription> 元素之 <Summary> 元素子系。

此範例使用父軸傳回 <Feature> 元素的父系,並擷取 <ProductDescription> 元素的 <Summary> 元素子系。

SELECT CatalogDescription.query('
declare namespace PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
  
/child::PD:ProductDescription/child::PD:Features/parent::PD:ProductDescription/child::PD:Summary
')
FROM   Production.ProductModel
WHERE  ProductModelID=19 
 

在此查詢範例中,路徑運算式使用 parent 軸。您可以重寫沒有父軸的運算式,如下列所示:

  /child::PD:ProductDescription[child::PD:Features]/child::PD:Summary

在下列範例中提供了更有用的父軸範例。

每個儲存在 ProductModel 資料表中 CatalogDescription 資料行的產品型號目錄描述都有 <ProductDescription> 元素,該元素有 ProductModelID 屬性與 <Features> 子元素,如下列片段所示:

<ProductDescription ProductModelID="..." >
  ...
  <Features>
    <Feature1>...</Feature1>
    <Feature2>...</Feature2>
   ...
</ProductDescription>

此查詢在 FLWOR 陳述式中設定了 iterator 變數、$f,以傳回 <Features> 元素的元素子系。如需詳細資訊,請參閱<FLWOR 陳述式與反覆運算 (XQuery)>。對於每個功能,return 子句會以下列形式建構 XML:

<Feature ProductModelID="...">...</Feature>
<Feature ProductModelID="...">...</Feature>

若要對每個 <Feature> 元素加入 ProductModelID,就會指定 parent 軸:

SELECT CatalogDescription.query('
declare namespace PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
declare namespace wm="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain";
  for $f in /child::PD:ProductDescription/child::PD:Features/child::*
  return
   <Feature
     ProductModelID="{ ($f/parent::PD:Features/parent::PD:ProductDescription/attribute::ProductModelID)[1]}" >
          { $f }
   </Feature>
')
FROM  Production.ProductModel
WHERE ProductModelID=19

以下是部份結果:

<Feature ProductModelID="19">
  <wm:Warranty 
   xmlns:wm="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain">
    <wm:WarrantyPeriod>3 years</wm:WarrantyPeriod>
    <wm:Description>parts and labor</wm:Description>
  </wm:Warranty>
</Feature>
<Feature ProductModelID="19">
  <wm:Maintenance 
   xmlns:wm="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain">
    <wm:NoOfYears>10 years</wm:NoOfYears>
    <wm:Description>maintenance contract available through your dealer 
                  or any AdventureWorks retail store.</wm:Description>
  </wm:Maintenance>
</Feature>
<Feature ProductModelID="19">
  <p1:wheel 
   xmlns:p1="https://www.adventure-works.com/schemas/OtherFeatures">
      High performance wheels.
  </p1:wheel>
</Feature>

請注意在路徑運算式中加入了述詞 [1],以確保會傳回單一值。