Share via


nodes() yöntemi (xml veri türü)

Nodes() yöntemi yararlı parçalayıp istediğinizde bir xmlilişkisel veri içine veri türü örneği. Yeni bir satıra eşlenen düğümleri tanımlamak sağlar.

Her xmlveri türü örneği olan bir örtülü olarak sağlanan içerik düğümünün. Bir sütun veya değişken depolanan xml örneği için bu belge düğümü var. Belge düğümü üst kısmındaki örtülü bir düğümdür her xmlveri türü örneği.

Sonucu nodes() mantıksal kopya özgün xml örnekleri içeren bir satır kümesi bir yöntemdir. Böylece sonraki sorguları bu bağlam düğüm göreli olarak gidebilirsiniz bu mantıksal kopyaları, her satır örneği olan içerik düğümü sorgu ifadesi ile tanımlanan düğümlerden ayarlanır.

Satır kümesi birden fazla değer alabilir. Örneğin, uygulayabileceğiniz value() yöntemi tarafından döndürülen satır kümesi nodes() ve birden çok değer özgün xml örneğinden almak. Not value() xml örneğine uygulanan yöntemi, tek bir değeri döndürür.

Sözdizimi

nodes (XQuery) as Table(Column)

Bağımsız değişkenler

  • XQuery
    Bir dize, bir XQuery ifade olduğunu. Bu inşa düğümler düğüm sorgu ifadesi oluşturur, sonuç kümesi içinde sunulur. Sorgu ifadesi içinde boş bir sıra kaynaklanıyorsa, satır kümesi boş olacaktır. Sorgu ifadesi statik olarak düğüm yerine atomik değerleri içeren bir dizi sonuçlanırsa, statik bir hata ortaya çıkar.

  • Table(Column)
    Tablo adı ve sütun adı sonuç satır kümesi için olduğunu.

Açıklamalar

Örneğin, aşağıdaki tabloda olduğu varsayılmaktadır:

T (ProductModelID int, Instructions xml)

T (ProductModelID int, Instructions xml)

Aşağıdaki üretim yönergeleri belge tablosunda depolanır. Yalnızca bir parçası gösterilmektedir. Belge içindeki üç üretim yerleri olduğunu unutmayın.

<root>
  <Location LocationID="10"...>
     <step>...</step>
     <step>...</step>
      ...
  </Location>
  <Location LocationID="20" ...>
       ...
  </Location>
  <Location LocationID="30" ...>
       ...
  </Location>
</root>

<root>
  <Location LocationID="10"...>
     <step>...</step>
     <step>...</step>
      ...
  </Location>
  <Location LocationID="20" ...>
       ...
  </Location>
  <Location LocationID="30" ...>
       ...
  </Location>
</root>

A nodes()yöntemi çağırma sorgu ifadesi ile /root/Locationile üç satır satır kümesi döndürür, her mantıksal bir kopyasını özgün xml belge ve içerik madde içeren birine ayarlayın <Location>düğüm:

Product
ModelID      Instructions
----------------------------------
1       <root>
             <Location LocationID="20" ... />
             <Location LocationID="30" .../></root>
1      <root><Location LocationID="10" ... />
             
             <Location LocationID="30" .../></root>
1      <root><Location LocationID="10" ... />
             <Location LocationID="20" ... />
             </root>

Product
ModelID      Instructions
----------------------------------
1       <root>
             <Location LocationID="20" ... />
             <Location LocationID="30" .../></root>
1      <root><Location LocationID="10" ... />
             
             <Location LocationID="30" .../></root>
1      <root><Location LocationID="10" ... />
             <Location LocationID="20" ... />
             </root>

Sen o zaman bu satır kümesi kullanarak sorgu xmlveri türü yöntemleri. Aşağıdaki sorgu oluşturulan her satır için bağlam öğenin alt ağaçta çıkarır:

SELECT T2.Loc.query('.')
FROM   T
CROSS APPLY Instructions.nodes('/root/Location') as T2(Loc) 

SELECT T2.Loc.query('.')
FROM   T
CROSS APPLY Instructions.nodes('/root/Location') as T2(Loc) 

Sonuç şudur:

ProductModelID  Instructions
----------------------------------
1        <Location LocationID="10" ... />
1        <Location LocationID="20" ... />
1        <Location LocationID="30" .../>

ProductModelID  Instructions
----------------------------------
1        <Location LocationID="10" ... />
1        <Location LocationID="20" ... />
1        <Location LocationID="30" .../>

Döndürülen satır kümesi türü bilgileri ettirmiştir unutmayın. Sen-ebilmek uygulamak xmlveri türü yöntemleri gibi query(), value(), exist(), ve nodes(), sonucunu bir nodes() yöntemi. Ancak, uygulayamazsınız modify() yöntemi xml örneği değiştirmek için.

Ayrıca, içerik düğümünün satır kümesi hayata olamaz. Başka bir deyişle, bir select deyiminde kullanamazsınız. Ancak, IT IS null ve count(*) kullanabilirsiniz.

Kullanma senaryoları nodes() yöntemini kullanarak aynı olan openxml (Transact-sql). Bu, xml satır kümesi bir görünümünü sağlar. Ancak, kullandığınız zaman imleçler kullanmak gerekmez nodes() yöntemi xml belge birden fazla satır içeren bir tablo.

Döndürülen satır kümesi Not nodes() isimsiz bir satır kümesi bir yöntemdir. Bu nedenle, bunu açıkça kenar yumuşatma kullanarak adlandırılmalıdır.

Nodes() işlevi kullanıcı tanımlı bir işlev sonuçlarına doğrudan uygulanır. Kullanmak için nodes() fonksiyon skalar kullanıcı tanımlı bir işlev sonucu ile sonucu bir değişkene kullanıcı tanımlı bir işlev atamak veya bir sütun diğer adı için kullanıcı tanımlı işlevin dönüş değeri atayın ve sonra diğer--dan seçmek için çapraz apply türetilmiş bir tablo kullanın.

Aşağıdaki örnek, kullanmak için bir yol gösterir CROSS APPLYkullanıcı tanımlı bir işlev sonucunda seçilecek.

USE AdventureWorks;
GO

CREATE FUNCTION XTest()
RETURNS xml
AS
BEGIN
RETURN '<document/>';
END;
GO


SELECT A2.B.query('.')
FROM
(SELECT dbo.XTest()) AS A1(X) 
CROSS APPLY X.nodes('.') A2(B);
GO

DROP FUNCTION XTest;
GO

USE AdventureWorks;
GO

CREATE FUNCTION XTest()
RETURNS xml
AS
BEGIN
RETURN '<document/>';
END;
GO


SELECT A2.B.query('.')
FROM
(SELECT dbo.XTest()) AS A1(X) 
CROSS APPLY X.nodes('.') A2(B);
GO

DROP FUNCTION XTest;
GO

Örnekler

Bir xml türü değişkeni karşı nodes() yöntemini kullanarak

Aşağıdaki örnek, orada olan bir xml belgesini bir <Root> en üst düzey öğe ve üç <row> alt öğeleri. Sorgu kullanır nodes()yöntemi, her ayrı içerik düğümler için <row>öğesi. nodes()Yöntemi ile üç satır satır kümesi döndürür. Her satır özgün xml mantıksal bir kopyasını farklı bir tanımlama her içerik düğümü ile sahip <row> özgün belgedeki öğe.

Sorgu düğümü her satırdan sonra döndürür:

DECLARE @x xml 
SET @x='<Root>
    <row id="1"><name>Larry</name><oflw>some text</oflw></row>
    <row id="2"><name>moe</name></row>
    <row id="3" />
</Root>'
SELECT T.c.query('.') AS result
FROM   @x.nodes('/Root/row') T(c)
GO

DECLARE @x xml 
SET @x='<Root>
    <row id="1"><name>Larry</name><oflw>some text</oflw></row>
    <row id="2"><name>moe</name></row>
    <row id="3" />
</Root>'
SELECT T.c.query('.') AS result
FROM   @x.nodes('/Root/row') T(c)
GO

Sonuç aşağıdadır. Bu örnekte, sorgu yöntemi bağlam öğeyi hem de içeriğini döndürür:

 <row id="1"><name>Larry</name><oflw>some text</oflw></row>
 <row id="2"><name>moe</name></row>
 <row id="3"/>
 <row id="1"><name>Larry</name><oflw>some text</oflw></row>
 <row id="2"><name>moe</name></row>
 <row id="3"/>

Bağlam düğümlerinde üst erişeni uygulama döndürür <Root> için üç öğe:

SELECT T.c.query('..') AS result
FROM   @x.nodes('/Root/row') T(c)
go

SELECT T.c.query('..') AS result
FROM   @x.nodes('/Root/row') T(c)
go

Sonuç şudur:

<Root>
    <row id="1"><name>Larry</name><oflw>some text</oflw></row>
    <row id="2"><name>moe</name></row>
    <row id="3" />
</Root>
<Root>
    <row id="1"><name>Larry</name><oflw>some text</oflw></row>
    <row id="2"><name>moe</name></row>
    <row id="3" />
</Root>
<Root>
    <row id="1"><name>Larry</name><oflw>some text</oflw></row>
    <row id="2"><name>moe</name></row>
    <row id="3" />
</Root>

<Root>
    <row id="1"><name>Larry</name><oflw>some text</oflw></row>
    <row id="2"><name>moe</name></row>
    <row id="3" />
</Root>
<Root>
    <row id="1"><name>Larry</name><oflw>some text</oflw></row>
    <row id="2"><name>moe</name></row>
    <row id="3" />
</Root>
<Root>
    <row id="1"><name>Larry</name><oflw>some text</oflw></row>
    <row id="2"><name>moe</name></row>
    <row id="3" />
</Root>

Xml türü sütun karşı nodes() yöntemi belirtme

Bisiklet üretim yönergeleri ve bu örnekte kullanılan yönergeleri depolanan xmltürü sütununu ProductModel tablosu.

Aşağıdaki örnekte, nodes()yöntemi karşı belirtilen Instructionssütununda xmlyazın ProductModeltablosu.

nodes()Yöntemini ayarlar <Location>öğeleri belirleyerek ve bağlam düğüm /MI:root/MI:Locationyolu Özgün belge, her mantıksal kopyalarını elde edilen satır kümesi içerir <Location> düğüm belgede, bağlam düğüm kümesi <Location> öğesi. Bu nedenle, nodes()bir dizi işlev verir <Location>içerik düğümü.

query()Yöntemi bu satır kümesi karşı ister self::nodeve bu nedenle döndürür <Location>her satır öğesinde.

Bu örnekte, sorgu her ayarlar <Location> öğesi olarak üretim yönergeleri bağlam düğüm belge, belirli bir ürün modeli. Bu bağlamda düğümler aşağıdaki gibi değerleri almak için kullanabilirsiniz:

  • Her yerde kimlikleri bulmak<Location>

  • Üretim adımları almak (<step> alt öğeler) her <Location>

Bu sorgu bağlamı öğenin içinde döndürür kısaltılmış sözdizimi '.'için self::node(), içinde belirtilen query()yöntemi.

Aşağıdakilere dikkat edin:

  • nodes()Yöntem yönergeleri sütuna uygulanır ve bir satır kümesi döndürür T (C). Bu satır kümesi mantıksal özgün kopyalarını içeren üretim yönergeleri belge ile /root/Locationiçerik öğe olarak.

  • apply geçerlidir nodes()her satır için Instructionstablo ve yalnızca bir sonuç kümesi üretmek satırları döndürür.

    SELECT C.query('.') as result
    FROM Production.ProductModel
    CROSS APPLY Instructions.nodes('
    declare namespace MI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
    /MI:root/MI:Location') as T(C)
    WHERE ProductModelID=7
    
    SELECT C.query('.') as result
    FROM Production.ProductModel
    CROSS APPLY Instructions.nodes('
    declare namespace MI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
    /MI:root/MI:Location') as T(C)
    WHERE ProductModelID=7
    

    Bu kısmi bir sonucudur:

    <MI:Location LocationID="10"  ...>
       <MI:step ... />
          ...
    </MI:Location>
    <MI:Location LocationID="20"  ... >
        <MI:step ... />
          ...
    </MI:Location>
    ...
    
    <MI:Location LocationID="10"  ...>
       <MI:step ... />
          ...
    </MI:Location>
    <MI:Location LocationID="20"  ... >
        <MI:step ... />
          ...
    </MI:Location>
    ...
    

Nodes() başka nodes() yöntemi tarafından döndürülen satır kümesi uygulama

xml belgeleri üretim yönergeleri için aşağıdaki kod sorgular Instructionssütununda ProductModeltablosu. Sorgu, ürün modeli kimliği, üretim yerleri ve üretim adımları içeren bir satır kümesi döndürür.

Aşağıdakilere dikkat edin:

  • nodes()Yöntemi için uygulanan Instructionssütun ve döner T1 (Locations)satır kümesi. Bu satır kümesi mantıksal özgün kopyalarını içeren yönergeleri belgesi ile üretim /root/Locationöğesi olarak madde bağlamında.

  • nodes()uygulanan T1 (Locations)satır kümesi döndürür T2 (steps)satır kümesi. Bu satır kümesi mantıksal özgün kopyalarını içeren yönergeleri belgesi ile üretim /root/Location/stepöğesi olarak madde bağlamında.

SELECT ProductModelID, Locations.value('./@LocationID','int') as LocID,
steps.query('.') as Step       
FROM Production.ProductModel       
CROSS APPLY Instructions.nodes('       
declare namespace MI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";       
/MI:root/MI:Location') as T1(Locations)       
CROSS APPLY T1.Locations.nodes('       
declare namespace MI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";       
./MI:step ') as T2(steps)       
WHERE ProductModelID=7       
GO       
SELECT ProductModelID, Locations.value('./@LocationID','int') as LocID,
steps.query('.') as Step       
FROM Production.ProductModel       
CROSS APPLY Instructions.nodes('       
declare namespace MI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";       
/MI:root/MI:Location') as T1(Locations)       
CROSS APPLY T1.Locations.nodes('       
declare namespace MI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";       
./MI:step ') as T2(steps)       
WHERE ProductModelID=7       
GO       

Sonuç şudur:

ProductModelID LocID Step       
----------------------------       
7      10   <step ... />       
7      10   <step ... />       
...       
7      20   <step ... />       
7      20   <step ... />       
7      20   <step ... />       
...       
ProductModelID LocID Step       
----------------------------       
7      10   <step ... />       
7      10   <step ... />       
...       
7      20   <step ... />       
7      20   <step ... />       
7      20   <step ... />       
...       

Sorgu beyan MIöneki iki kez. Bunun yerine, sen-ebilmek kullanma WITH XMLNAMESPACESbir kez önek bildirmek ve sorgu kullanabilirsiniz:

WITH XMLNAMESPACES (
   'https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions'  AS MI)

SELECT ProductModelID, Locations.value('./@LocationID','int') as LocID,
steps.query('.') as Step       
FROM Production.ProductModel       
CROSS APPLY Instructions.nodes('       
/MI:root/MI:Location') as T1(Locations)       
CROSS APPLY T1.Locations.nodes('       
./MI:step ') as T2(steps)       
WHERE ProductModelID=7       
GO  
WITH XMLNAMESPACES (
   'https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions'  AS MI)

SELECT ProductModelID, Locations.value('./@LocationID','int') as LocID,
steps.query('.') as Step       
FROM Production.ProductModel       
CROSS APPLY Instructions.nodes('       
/MI:root/MI:Location') as T1(Locations)       
CROSS APPLY T1.Locations.nodes('       
./MI:step ') as T2(steps)       
WHERE ProductModelID=7       
GO  

Ayrıca bkz.

Kavramlar

İle sorguları WITH xmlnamespaces NameSpaces eklemek

xml veri örnekleri oluşturma

Diğer Kaynaklar

XML veri türü yöntemleri