id 函数 (XQuery)
返回一组具有 xs:ID 值的元素节点,这些 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 示例,这些实例存储在 AdventureWorks2012 数据库内不同的 xml 类型列中。
A.基于 IDREF 属性值检索元素
下面的示例使用 fn:id 来基于 IDREF manager 属性检索 <employee> 元素。 在此示例中,manager 属性是一个 IDREF 类型的属性,eid 属性是一个 ID 类型的属性。
对于特定的 manager 属性值,id() 函数查找其 ID 类型属性值匹配输入 IDREF 值的 <employee> 元素。 换言之,对于某个特定雇员,id() 函数返回雇员经理。
在该示例中执行下列操作:
创建一个 XML 架构集合。
使用 XML 架构集合创建类型化的 xml 变量。
查询检索具有 <employee> 元素的 manager IDREF 属性引用的 ID 属性值的元素。
-- 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() 函数。 然后,该函数查找其 OrderID 属性值匹配 id() 函数的输入的 <Order> 元素。
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* 的子类型。