Aracılığıyla paylaş


kimliği işlev (XQuery)

Xs: ID değerleri içeren bir veya daha fazla sağlanan xs: IDREF değerlerinin değerlerle eşleşen öğe düğümlerinin sırasını döndürür $arg.

Sözdizimi

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

Bağımsız değişkenler

  • $arg
    Bir veya daha fazla xs: IDREF değerleri.

Açıklamalar

İşlevinin sonucu öğeleri xml örneğinde eşit bir veya daha fazla xs:IDREFs için aday xs:IDREFs listesinde bir xs: ID değeri olan belge sipariş dizisidir.

Xs: IDREF değeri herhangi bir öğe eşleşmezse, işlev boş sırası döndürür.

Örnekler

Bu konu çeşitli depolanan xml örnekleri karşı XQuery örnekler sağlar xml yazın sütunlarında AdventureWorks2012 veritabanı. Bu sütunlar genel bakış için bkz: DEL - xml Data Type Representation in the AdventureWorks2008R2 Database.

A.IDREF öznitelik değerini temel alan öğeleri alınıyor

Aşağıdaki örnek almak için fn:id kullanır <employee> elemanları, IDREF Yöneticisi özniteliğe dayalı. Bu örnekte, Yöneticisi IDREF türünü öznitelik özniteliğidir ve eid ID türünün özniteliği özniteliğidir.

Belirli Yöneticisi öznitelik değeri, id() işlevi bulur <employee> ID türünün özniteliği değeri ile eşleşen girdi IDREF değeri öğesi. Diğer bir deyişle, belirli bir çalışana ait id() işlevi çalışan Yöneticisi.

Bu örnekte olur:

  • xml şema koleksiyonu oluşturulur.

  • Bir yazılı xml değişken xml şema koleksiyonu kullanarak oluşturulur.

  • Sorgu tarafından başvurulan bir ID öznitelik değeri olan öğeyi alır Müdürü IDREF öznitelik <employee> öğesi.

-- 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

-- 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

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

Sorgu döndürür "Dave" değer olarak. Bu Dave Joe's yöneticisi olduğunu gösterir.

B.B.OrderList IDREFS öznitelik değerini temel alan öğeleri alınıyor

Aşağıdaki örnekte, B.OrderList özniteliği <Customer> IDREFS türü öznitelik öğedir. Bu, belirli bir müşteriye ait sipariş kimlikleri listelenir. Her sipariş kimliği, orada bir <Order> altında öğe alt <Customer> sipariş değeri.

Sorgu ifadesi data(CustOrders:Customers/Customer[1]/@OrderList)[1], ilk değer IDRES listesinden ilk müşterinin alır. Bu değer daha sonra geçer id() işlevi. İşlev sonra bulur <Order> öğesinin SiparişNo özniteliği değeri ile eşleşen girdi id() işlevi.

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>

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>

Uygulama kısıtlamaları

Bu sınırlamalar şunlardır:

  • SQL Serveriki bağımsız sürümünü desteklemiyor id().

  • SQL Serverbağımsız değişken türü gerektirir id() alt türü olan xs:IDREF* için.

Ayrıca bkz.

Başvuru

Dizileri fonksiyonlar