id 函數 (XQuery)

傳回含有 xs:ID 值的元素節點序列,這些值符合 $arg 所提供的一或多個 xs:IDREF 值。

語法

fn:id($arg as xs:IDREF*) as element()*

引數

  • $arg
    一個或多個 xs:IDREF 值。

備註

函數的結果是依照文件順序之 XML 執行個體中的元素序列,其中 xs:ID 值等於適用 xs:IDREF 的清單中之一或多個 xs:IDREF。

如果 xs:IDREF 值與任何元素不相符,函數會傳回空白時序。

範例

本主題是針對 XML 執行個體提供 XQuery 範例,這些執行個體是儲存在 AdventureWorks 資料庫的數個 xml 類型資料行中。如需這些資料行中每個資料行的概觀,請參閱<在 AdventureWorks 資料庫中的 xml 資料類型表示法>。

A. 根據 IDREF 屬性值擷取元素

下列範例是根據 IDREF 經理屬性來使用 fn:id 以擷取 <employee> 元素。在此範例中,經理屬性是 IDREF 類型屬性,而且 eid 屬性是 ID 類型屬性。

對於特定的經理屬性值,id() 函數會尋找 <employee> 元素,它的 ID 類型屬性值符合輸入 IDEF 值。換句話說,對於特定的員工而言,id() 函數會傳回員工的經理。

在範例中執行了下列動作:

  • 建立 XML 結構描述集合。

  • 使用 XML 結構描述集合建立具類型的 xml 變數。

  • 該查詢會擷取含有 ID 屬性值的元素,該值將由 <employee> 元素的 manager IDREF 屬性參考。

-- If exists, drop the XML schema collection (SC).
-- drop xml schema collection SC
-- go

create xml schema collection SC as
'<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:e="emp" targetNamespace="emp">
            <element name="employees" type="e:EmployeesType"/>
            <complexType name="EmployeesType">
                 <sequence>
                      <element name="employee" type="e:EmployeeType" minOccurs="0" maxOccurs="unbounded" />
                 </sequence>
            </complexType>  
 
            <complexType name="EmployeeType">
                        <attribute name="eid" type="ID" />
                        <attribute name="name" type="string" />
                        <attribute name="manager" type="IDREF" />
            </complexType>       
</schema>'
go

declare @x xml(SC)
set @x='<e:employees xmlns:e="emp">
<employee eid="e1" name="Joe" manager="e10" />
<employee eid="e2" name="Bob" manager="e10" />
<employee eid="e10" name="Dave" manager="e10" />
</e:employees>'
 
select @x.value(' declare namespace e="emp"; 
 (fn:id(e:employees/employee[@name="Joe"]/@manager)/@name)[1]', 'varchar(50)') 
Go

該查詢所傳回的值為 "Dave"。這表示 Dave 是 Joe 的經理。

B. 根據 OrderList IDREFS 屬性值擷取元素

在下列範例中,<Customer> 元素的 OrderList 屬性是 IDREFS 類型屬性。它列出特定客戶的 id 順序。對於每個 id 順序,在 <Customer> 之下有一個 <Order> 元素子系以提供順序值。

查詢運算式 data(CustOrders:Customers/Customer[1]/@OrderList)[1] 會從第一個客戶的 IDRES 清單擷取第一個值。此值接著會傳遞給 id() 函數。該函數接著會尋找 <Order> 元素,它的 OrderID 屬性值符合 id() 函數的輸入。

drop xml schema collection SC
go
create xml schema collection SC as
'<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:Customers="Customers" targetNamespace="Customers">
            <element name="Customers" type="Customers:CustomersType"/>
            <complexType name="CustomersType">
                        <sequence>
                            <element name="Customer" type="Customers:CustomerType" minOccurs="0" maxOccurs="unbounded" />
                        </sequence>
            </complexType>
             <complexType name="OrderType">
                <sequence minOccurs="0" maxOccurs="unbounded">
                            <choice>
                                <element name="OrderValue" type="integer" minOccurs="0" maxOccurs="unbounded"/>
                            </choice>
                </sequence>                                           
                <attribute name="OrderID" type="ID" />
            </complexType>

            <complexType name="CustomerType">
                <sequence minOccurs="0" maxOccurs="unbounded">
                            <choice>
                                <element name="spouse" type="string" minOccurs="0" maxOccurs="unbounded"/>
                                <element name="Order" type="Customers:OrderType" minOccurs="0" maxOccurs="unbounded"/>
                            </choice>
                </sequence>                                           
                <attribute name="CustomerID" type="string" />
                <attribute name="OrderList" type="IDREFS" />
            </complexType>
 </schema>'
go
declare @x xml(SC)
set @x='<CustOrders:Customers xmlns:CustOrders="Customers">
                <Customer CustomerID="C1" OrderList="OrderA OrderB"  >
                              <spouse>Jenny</spouse>
                                <Order OrderID="OrderA"><OrderValue>11</OrderValue></Order>
                                <Order OrderID="OrderB"><OrderValue>22</OrderValue></Order>

                </Customer>
                <Customer CustomerID="C2" OrderList="OrderC OrderD" >
                                <spouse>John</spouse>
                                <Order OrderID="OrderC"><OrderValue>33</OrderValue></Order>
                                <Order OrderID="OrderD"><OrderValue>44</OrderValue></Order>

                        </Customer>
                <Customer CustomerID="C3"  OrderList="OrderE OrderF" >
                                <spouse>Jane</spouse>
                                <Order OrderID="OrderE"><OrderValue>55</OrderValue></Order>
                                <Order OrderID="OrderF"><OrderValue>55</OrderValue></Order>
                </Customer>
                <Customer CustomerID="C4"  OrderList="OrderG"  >
                                <spouse>Tim</spouse>
                                <Order OrderID="OrderG"><OrderValue>66</OrderValue></Order>
                        </Customer>
                <Customer CustomerID="C5"  >
                </Customer>
                <Customer CustomerID="C6" >
                </Customer>
                <Customer CustomerID="C7"  >
                </Customer>
</CustOrders:Customers>'
select @x.query('declare namespace CustOrders="Customers";
  id(data(CustOrders:Customers/Customer[1]/@OrderList)[1])')

-- result
<Order OrderID="OrderA">
  <OrderValue>11</OrderValue>
</Order>

實作限制

以下為其限制:

  • SQL Server 不支援 id() 的兩個引數版本。

  • SQL Server 需要 id() 的引數類型為 xs:IDREF* 的子類型。

請參閱

參考