Share via


處理 XQuery 中的命名空間

適用於:SQL Server

本主題提供在查詢中處理命名空間的範例。

範例

A. 宣告命名空間

下列查詢會擷取特定產品模型的製造步驟。

SELECT Instructions.query('  
     declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";  
        /AWMI:root/AWMI:Location[1]/AWMI:step  
    ') as x  
FROM Production.ProductModel  
WHERE ProductModelID=7  

以下是部份結果:

<AWMI:step xmlns:AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions">Insert <AWMI:material>aluminum sheet MS-2341</AWMI:material> into the <AWMI:tool>T-85A framing tool</AWMI:tool>. </AWMI:step>  
...  

請注意, namespace 關鍵字是用來定義新的命名空間前置詞 「AWMI:」。 然後,這個前置詞必須在查詢中用於屬於該命名空間範圍內的所有元素。

B. 宣告預設命名空間

在上一個查詢中,已定義新的命名空間前置詞。 然後,該前置詞必須用於查詢中,以選取預期的 XML 結構。 或者,您可以將命名空間宣告為預設命名空間,如下列修改過的查詢所示:

SELECT Instructions.query('  
     declare default element namespace "https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";  
        /root/Location[1]/step  
    ') as x  
FROM Production.ProductModel  
where ProductModelID=7  

以下是結果:

<step xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions">Insert <material>aluminum sheet MS-2341</material> into the <tool>T-85A framing tool</tool>. </step>  
...  

請注意,在此範例中,會建立定義 命名空間 、 "https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions" ,以覆寫預設值或空白命名空間。 因此,您在用來查詢的路徑運算式中不再有命名空間前置詞。 您也不再在結果中顯示的專案名稱中具有命名空間前置詞。 此外,預設命名空間會套用至所有元素,但不會套用至其屬性。

C. 在 XML 建構中使用命名空間

當您定義新的命名空間時,它們不僅會納入查詢範圍,而且會納入建構範圍。 例如,在建構 XML 時,您可以使用 「 declare namespace ... 」 宣告來定義新的命名空間,然後將該命名空間與建構的任何元素和屬性搭配使用,以出現在查詢結果中。

SELECT CatalogDescription.query('  
     declare default element namespace "https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";  
     declare namespace myNS="uri:SomeNamespace";<myNS:Result>  
          { /ProductDescription/Summary }  
       </myNS:Result>  
  
    ') as Result  
FROM Production.ProductModel  
where ProductModelID=19  

以下是結果:

  
      <myNS:Result xmlns:myNS="uri:SomeNamespace">  
  <Summary xmlns="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription">  
   <p1:p xmlns:p1="http://www.w3.org/1999/xhtml">  
     Our top-of-the-line competition mountain bike. Performance-enhancing   
     options include the innovative HL Frame, super-smooth front   
     suspension, and traction for all terrain.</p1:p>  
  </Summary>  
</myNS:Result>  

或者,您也可以在 XML 建構的每個點明確定義命名空間,如下列查詢所示:

SELECT CatalogDescription.query('  
     declare default element namespace "https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";  
       <myNS:Result xmlns:myNS="uri:SomeNamespace">  
          { /ProductDescription/Summary }  
       </myNS:Result>  
    ') as Result  
FROM Production.ProductModel  
where ProductModelID=19  

D. 使用預設命名空間建構

您也可以定義用於建構 XML 的預設命名空間。 例如,下列查詢示範如何指定預設命名空間 「uri:SomeNamespace」\,以做為建構之本機具名元素的預設值,例如 <Result> 元素。

SELECT CatalogDescription.query('  
      declare namespace PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";  
      declare default element namespace "uri:SomeNamespace";<Result>  
          { /PD:ProductDescription/PD:Summary }  
       </Result>  
  
    ') as Result  
FROM Production.ProductModel  
where ProductModelID=19  

以下是結果:

  
      <Result xmlns="uri:SomeNamespace">  
  <PD:Summary xmlns:PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription">  
   <p1:p xmlns:p1="http://www.w3.org/1999/xhtml">  
         Our top-of-the-line competition mountain bike. Performance-  
         enhancing options include the innovative HL Frame, super-smooth   
         front suspension, and traction for all terrain.</p1:p>  
  </PD:Summary>  
</Result>  

請注意,藉由覆寫預設專案命名空間或空白命名空間,建構 XML 中的所有本機具名元素後續都會系結至覆寫的預設命名空間。 因此,如果您需要彈性建構 XML 以利用空命名空間,請勿覆寫預設專案命名空間。

另請參閱

使用 WITH XMLNAMESPACES 將命名空間加入至查詢
XML 資料 (SQL Server)
Xquery 語言參考 (SQL Server)