路徑運算式 - 指定軸

適用於:SQL Server

路徑運算式中的軸步驟包含下列元件:

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

SQL Server 中的 XQuery 實作支援下列座標軸步驟:

Axis 描述
孩子 傳回內容節點的子系。
後代 傳回內容節點的所有子代。
parent 傳回內容節點的父代。
attribute 傳回內容節點的屬性。
自我 傳回內容節點本身。
descendant-or-self 傳回內容節點和內容節點的所有子代。

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

相對路徑運算式 child::root/child::Location/attribute::LocationID 有三個步驟。 前兩個步驟分別指定 child 座標軸,而第三個步驟則 attribute 指定座標軸。 針對 Production.ProductModel 資料表中的 製造指令 XML 檔執行時,運算式會 LocationID 傳回根 > 元素之 < Location > 元素節點子系的 < 屬性。

範例

本主題中的查詢範例會針對 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  

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

  • query()xml 資料類型的 方法會指定路徑運算式。

  • 路徑運算式中的兩個步驟都會指定 child 座標軸和節點名稱,以及 FeaturesProductDescription 作為節點測試。 如需節點測試的相關資訊,請參閱 在路徑運算式步驟 中指定節點測試。

B. 指定子代和子代或自我軸

下列範例使用子代和子代或自我軸。 此範例中的查詢會針對 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  

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

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

在此運算式中,如果您指定路徑運算式的子系座標軸,

/child::a/child::b/descendant::*,您要求元素節點的所有子代 <b> 。

節點測試中的星號 \ 代表節點名稱做為節點測試。 因此,子系座標軸的主要節點類型專案節點會決定傳回的節點類型。 也就是說,運算式會傳回所有元素 nodes。。 不會傳回文位元組點。 如需主要節點類型及其與節點測試關聯性的詳細資訊,請參閱 在路徑運算式步驟 中指定節點測試主題。

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

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

如果您指定子代或自我座標軸,而不是子系座標軸, /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. 指定父軸

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

這個範例會使用父軸傳回專案的父 <>Feature 代,並擷取 Summary>< 元素的 <ProductDescription> 元素子系。

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 語句中設定反覆運算器變數 $f ,以傳回元素的 <Features> 元素子系。 如需詳細資訊,請參閱 FLWOR 語句和反復專案 (XQuery) 。 針對每個功能, return 子句會以下列形式建構 XML:

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

若要為每個元素加入 ProductModelID<Feature> ,則會 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] 詞,以確保傳回單一值。