Aracılığıyla paylaş


xml yapım (XQuery)

XQuery, sen-ebilmek kullanma directve computedxml yapıları içinde bir sorgu oluşturmak için Kurucular.

[!NOT]

Yeteneği düğüm adları hesaplanmış Oluşturucusu kullandığınızda hesaplamak için önce desteklenmeyen SQL Server 2005. Bu nedenle, arasında hiçbir fark yoktur directve computedKurucular.

InSQL Server 2000, sadece inşaat modu, şerit ve XMLSpace İlkesi veya sınır boşluk ilke şeridi desteklenir.

Doğrudan oluşturucular kullanma

Doğrudan oluşturucular kullandığınızda, xml oluşturmak zaman xml benzeri sözdizimini belirlemek. Aşağıdaki örneklerde, xml yapım tarafından doğrudan oluşturucular gösterilmektedir.

Öğeleri oluşturmak yoluyla

xml işaretler kullanarak, öğelerin hazırlayabilirsiniz. Aşağıdaki örnek, doğrudan öğesi Oluşturucu ifadesini kullanır ve bir <ProductModel> öğesi. Yapılandırılmış öğe üç alt öğeleri vardır

  • Bir metin düğümü.

  • İki öğe düğümleri, <özeti> ve <özellikler>.

    • <Özeti> öğesi olan bir metin düğümü alt değeri olan "Bazı açıklama".

    • <Özellikler> öğesi olan üç öğe düğümü alt <renk>, <kilo>, ve <garanti>. Bu düğümlerin her birinde bir metin düğümü alt ve kırmızı, 25, 2 yıl parça ve işçilik, değerleri sırasıyla vardır.

declare @x xml
set @x=''
select @x.query('<ProductModel ProductModelID="111">
This is product model catalog description.
<Summary>Some description</Summary>
<Features>
  <Color>Red</Color>
  <Weight>25</Weight>
  <Warranty>2 years parts and labor</Warranty>
</Features></ProductModel>')

declare @x xml
set @x=''
select @x.query('<ProductModel ProductModelID="111">
This is product model catalog description.
<Summary>Some description</Summary>
<Features>
  <Color>Red</Color>
  <Weight>25</Weight>
  <Warranty>2 years parts and labor</Warranty>
</Features></ProductModel>')

Sonuç xml budur:

<ProductModel ProductModelID="111">
  This is product model catalog description.
  <Summary>Some description</Summary>
  <Features>
    <Color>Red</Color>
    <Weight>25</Weight>
    <Warranty>2 years parts and labor</Warranty>
  </Features>
</ProductModel>

<ProductModel ProductModelID="111">
  This is product model catalog description.
  <Summary>Some description</Summary>
  <Features>
    <Color>Red</Color>
    <Weight>25</Weight>
    <Warranty>2 years parts and labor</Warranty>
  </Features>
</ProductModel>

Bu örnekte gösterildiği gibi sabit ifadeler, öğeleri oluşturmak yararlı olsa da, bu XQuery dil özelliği gerçek gücünü dinamik olarak veri veritabanından ayıklar xml oluşturmak yeteneğidir. Kaşlı, sorgu deyimi belirtmek için kullanabilirsiniz. Sonuç xml, ifadenin değeri tarafından değiştirilir. Örneğin, aşağıdaki sorgu yapıları bir <NewRoot> öğesinin bir alt öğesi olan (<e>). Öğe değeri <e> ("{…} kaşlı ayraçlar içinde yol ifadesi belirterek hesaplanır").

DECLARE @x xml
SET @x='<root>5</root>'
SELECT @x.query('<NewRoot><e> { /root } </e></NewRoot>')

DECLARE @x xml
SET @x='<root>5</root>'
SELECT @x.query('<NewRoot><e> { /root } </e></NewRoot>')

Ayraçlar Bağlam Geçişi belirteçleri gibi hareket ve xml yapım sorgu için sorgu değerlendirme geçin. Bu durumda, XQuery yolu ifade parantez içinde /root, değerlendirilir ve sonuç görüneceği için yerine

Sonuç şudur:

<NewRoot>
  <e>
    <root>5</root>
  </e>
</NewRoot>

<NewRoot>
  <e>
    <root>5</root>
  </e>
</NewRoot>

Aşağıdaki sorguyu önceki bir benzer. Ancak, kaşlı ifade belirtir data()atomik değerini almak için işlevi <root>eleman ve inşa öğesine atar <e>.

DECLARE @x xml
SET @x='<root>5</root>'
DECLARE @y xml
SET @y = (SELECT @x.query('
                           <NewRoot>
                             <e> { data(/root) } </e>
                           </NewRoot>' ))
SELECT @y

DECLARE @x xml
SET @x='<root>5</root>'
DECLARE @y xml
SET @y = (SELECT @x.query('
                           <NewRoot>
                             <e> { data(/root) } </e>
                           </NewRoot>' ))
SELECT @y

Sonuç şudur:

<NewRoot>
  <e>5</e>
</NewRoot>

<NewRoot>
  <e>5</e>
</NewRoot>

Küme ayraçları metninizi Bağlam Geçişi belirteçleri yerine bir parçası olarak kullanmak istiyorsanız, onları kaçabilir "}}" veya "{{", bu örnekte gösterildiği gibi:

DECLARE @x xml
SET @x='<root>5</root>'
DECLARE @y xml
SET @y = (SELECT @x.query('
<NewRoot> Hello, I can use {{ and  }} as part of my text</NewRoot>'))
SELECT @y

DECLARE @x xml
SET @x='<root>5</root>'
DECLARE @y xml
SET @y = (SELECT @x.query('
<NewRoot> Hello, I can use {{ and  }} as part of my text</NewRoot>'))
SELECT @y

Sonuç şudur:

<NewRoot> Hello, I can use { and  } as part of my text</NewRoot>

<NewRoot> Hello, I can use { and  } as part of my text</NewRoot>

Aşağıdaki sorgu, öğeleri doğrudan öğesi Oluşturucu kullanarak oluşturmak, başka bir örnektir. Ayrıca, değeri <FirstLocation> öğesi ifade kaşlı yürüterek elde edilir. Sorgu ifadesinde, Production.ProductModel tablo yönergeleri sütun için üretim adımları ilk iş merkezi konumu döndürür.

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

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

Sonuç şudur:

<FirstLocation>
  <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>
  <AWMI:step xmlns:AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions">
      Attach <AWMI:tool>Trim Jig TJ-26</AWMI:tool> to the upper and lower right corners of the aluminum sheet. 
  </AWMI:step>
   ...
</FirstLocation>

<FirstLocation>
  <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>
  <AWMI:step xmlns:AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions">
      Attach <AWMI:tool>Trim Jig TJ-26</AWMI:tool> to the upper and lower right corners of the aluminum sheet. 
  </AWMI:step>
   ...
</FirstLocation>

Öğe içeriği xml inşaat

Aşağıdaki örnek doğrudan öğesi Oluşturucu kullanarak öğe içeriği oluştururken ifadeler davranışı göstermektedir. Aşağıdaki örnekte, bir ifade için doğrudan öğesi Oluşturucu belirtir. Bu ifade, sonuç xml içinde bir metin düğümü oluşturulur.

declare @x xml
set @x='
<root>
  <step>This is step 1</step>
  <step>This is step 2</step>
  <step>This is step 3</step>
</root>'
select @x.query('
<result>
 { for $i in /root[1]/step
    return string($i)
 }
</result>')

declare @x xml
set @x='
<root>
  <step>This is step 1</step>
  <step>This is step 2</step>
  <step>This is step 3</step>
</root>'
select @x.query('
<result>
 { for $i in /root[1]/step
    return string($i)
 }
</result>')

İfade değerlendirme çıkan atomik değer sıra metin düğüme sonucu gösterildiği gibi bitişik atomik değerleri arasında eklenen boşluk eklenir. Yapılandırılmış öğe bir çocuk vardır. Bu sonuçlarda gösterilen değeri içeren bir metin düğümü var.

<result>This is step 1 This is step 2 This is step 3</result>

<result>This is step 1 This is step 2 This is step 3</result>

Üç metin düğümleri, üreten üç ayrı ifadeler belirtirseniz, bir ifade yerine, birleştirme, sonuç XML tarafından bir tek metin düğümü bitişik metin düğümleri birleştirilir.

declare @x xml
set @x='
<root>
  <step>This is step 1</step>
  <step>This is step 2</step>
  <step>This is step 3</step>
</root>'
select @x.query('
<result>
 { string(/root[1]/step[1]) }
 { string(/root[1]/step[2]) }
 { string(/root[1]/step[3]) }
</result>')

declare @x xml
set @x='
<root>
  <step>This is step 1</step>
  <step>This is step 2</step>
  <step>This is step 3</step>
</root>'
select @x.query('
<result>
 { string(/root[1]/step[1]) }
 { string(/root[1]/step[2]) }
 { string(/root[1]/step[3]) }
</result>')

Yapılandırılmış öğe düğümü bir çocuk vardır. Bu sonuçlarda gösterilen değeri içeren bir metin düğümü var.

<result>This is step 1This is step 2This is step 3</result>

<result>This is step 1This is step 2This is step 3</result>

Öznitelikler oluşturma

Doğrudan öğesi Oluşturucu kullanarak öğeleri yapılandırırlar, ayrıca öğesi özniteliklerini xml benzeri sözdizimini kullanarak bu örnekte gösterildiği gibi belirtebilirsiniz:

declare @x xml
set @x=''
select @x.query('<ProductModel ProductModelID="111">
This is product model catalog description.
<Summary>Some description</Summary>
</ProductModel>')

declare @x xml
set @x=''
select @x.query('<ProductModel ProductModelID="111">
This is product model catalog description.
<Summary>Some description</Summary>
</ProductModel>')

Sonuç xml budur:

<ProductModel ProductModelID="111">
  This is product model catalog description.
  <Summary>Some description</Summary>
</ProductModel>

<ProductModel ProductModelID="111">
  This is product model catalog description.
  <Summary>Some description</Summary>
</ProductModel>

Yapılandırılmış öğe <ProductModel> ProductModelID özniteliği ve bu alt düğümleri vardır:

  • Bir metin düğümüThis is product model catalog description.

  • Bir eleman düğümü, <Summary>. Bir metin düğümü alt, bu düğümün sahip Some description.

Bir öznitelik yapılandırırlar, kaşlı bir ifade ile onun değerini belirtebilirsiniz. Bu durumda, ifadenin sonucu öznitelik değeri döndürülür.

Aşağıdaki örnekte, data()işlevi kesinlikle gerekli değildir. İfadenin değeri bir öznitelik için atadığınız çünkü data()örtülü olarak belirtilen ifade yazılı değerini almak için uygulanan.

DECLARE @x xml
SET @x='<root>5</root>'
DECLARE @y xml
SET @y = (SELECT @x.query('<NewRoot attr="{ data(/root) }" ></NewRoot>'))
SELECT @y

DECLARE @x xml
SET @x='<root>5</root>'
DECLARE @y xml
SET @y = (SELECT @x.query('<NewRoot attr="{ data(/root) }" ></NewRoot>'))
SELECT @y

Sonuç şudur:

<NewRoot attr="5" />

<NewRoot attr="5" />

Aşağıda, hangi ifadeleri LocationID ve SetupHrs özniteliği inşaat için belirtilen başka bir örnektir. Bu ifadeler xml yönergesi sütun karşı değerlendirilir. Yazılı ifade değerli öznitelikler için atanır.

SELECT Instructions.query('
    declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
        <FirstLocation 
         LocationID="{ (/AWMI:root/AWMI:Location[1]/@LocationID)[1] }"
         SetupHrs = "{ (/AWMI:root/AWMI:Location[1]/@SetupHours)[1] }" >
           { /AWMI:root/AWMI:Location[1]/AWMI:step }
        </FirstLocation> 
') as Result 
FROM  Production.ProductModel
where ProductModelID=7

SELECT Instructions.query('
    declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
        <FirstLocation 
         LocationID="{ (/AWMI:root/AWMI:Location[1]/@LocationID)[1] }"
         SetupHrs = "{ (/AWMI:root/AWMI:Location[1]/@SetupHours)[1] }" >
           { /AWMI:root/AWMI:Location[1]/AWMI:step }
        </FirstLocation> 
') as Result 
FROM  Production.ProductModel
where ProductModelID=7

Bu kısmi bir sonucudur:

<FirstLocation LocationID="10" SetupHours="0.5" >
  <AWMI:step … 
  </AWMI:step>
  ...
</FirstLocation>

<FirstLocation LocationID="10" SetupHours="0.5" >
  <AWMI:step … 
  </AWMI:step>
  ...
</FirstLocation>

Uygulama kısıtlamaları

Bu sınırlamalar şunlardır:

  • Birden çok veya karışık özniteliği ifadeleri (dize ve XQuery ifade) are değil taraftar. Örneğin, aşağıdaki sorgu gösterildiği gibi xml oluşturmak nerede Itemsabit ve değer 5bir sorgu ifadesinde değerlendirerek alınır:

    <a attr="Item 5" />
    
    <a attr="Item 5" />
    

    Aşağıdaki sorgu hata verir çünkü sabit dize karıştırma ifadeye sahip ({/ x}) ve bu desteklenmez:

    DECLARE @x xml
    SET @x ='<x>5</x>'
    SELECT @x.query( '<a attr="Item {/x}"/>' ) 
    
    DECLARE @x xml
    SET @x ='<x>5</x>'
    SELECT @x.query( '<a attr="Item {/x}"/>' ) 
    

    Bu durumda, aşağıdaki seçenekler vardır:

    • Öznitelik değeri iki atomik değerleri birleşimi tarafından oluştururlar. Bu atomik değerleri, öznitelik değerinin atomik değerlerin arasında bir boşluk içine dizileştirirler:

      SELECT @x.query( '<a attr="{''Item'', data(/x)}"/>' ) 
      
      SELECT @x.query( '<a attr="{''Item'', data(/x)}"/>' ) 
      

      Sonuç şudur:

      <a attr="Item 5" />
      
      <a attr="Item 5" />
      
    • Kullanım concat işlevi sonuç öznitelik değeri iki dize bağımsız değişkenler art arda bağlamak için:

      SELECT @x.query( '<a attr="{concat(''Item'', /x[1])}"/>' ) 
      
      SELECT @x.query( '<a attr="{concat(''Item'', /x[1])}"/>' ) 
      

      Bu durumda iki dize değerleri arasında eklenen boşluk yoktur. İki değer arasında bir boşluk istiyorsanız, açıkça sağlaması gerekir.

      Sonuç şudur:

      <a attr="Item5" />
      
      <a attr="Item5" />
      
  • Bir öznitelik değeri olarak birden çok deyimleri desteklenmez. Örneğin, aşağıdaki sorgu hata verir:

    DECLARE @x xml
    SET @x ='<x>5</x>'
    SELECT @x.query( '<a attr="{/x}{/x}"/>' )
    
    DECLARE @x xml
    SET @x ='<x>5</x>'
    SELECT @x.query( '<a attr="{/x}{/x}"/>' )
    
  • Türdeş olmayan diziler desteklenmez. Herhangi bir öznitelik değeri bir hata döndürür gibi heterojen bir sıra aşağıdaki örnekte gösterildiği gibi atama girişimi. Örneğin, heterojen bir sıra, bir dize "Öğesi" ve bir öğe <x>, öznitelik değeri olarak belirtilir:

    DECLARE @x xml
    SET @x ='<x>5</x>'
    select @x.query( '<a attr="{''Item'', /x }" />')
    
    DECLARE @x xml
    SET @x ='<x>5</x>'
    select @x.query( '<a attr="{''Item'', /x }" />')
    

    Eğer uyguladığınız data()sorgu işlevi çalışır, bu ifade, Atomik değeri alır çünkü /x, dize ile birleştirilmiş. Atomik değerleri dizisi aşağıdadır:

    SELECT @x.query( '<a attr="{''Item'', data(/x)}"/>' ) 
    
    SELECT @x.query( '<a attr="{''Item'', data(/x)}"/>' ) 
    

    Sonuç şudur:

    <a attr="Item 5" />
    
    <a attr="Item 5" />
    
  • Öznitelik düğümü sipariş statik tür denetlemesi sırasında yerine, seri hale getirme sırasında uygulanır. Örneğin, aşağıdaki sorgu başarısız olur sonra sigara öznitelik düğümü öznitelik eklemek çalışır.

    select convert(xml, '').query('
    element x { attribute att { "pass" }, element y { "Element text" }, attribute att2 { "fail" } }
    ')
    go
    
    select convert(xml, '').query('
    element x { attribute att { "pass" }, element y { "Element text" }, attribute att2 { "fail" } }
    ')
    go
    

    Yukarıdaki sorgu aşağıdaki hata döndürür:

    XML well-formedness check: Attribute cannot appear outside of element declaration. Rewrite your XQuery so it returns well-formed XML.
    
    XML well-formedness check: Attribute cannot appear outside of element declaration. Rewrite your XQuery so it returns well-formed XML.
    

Ad alanları ekleme

xml doğrudan oluşturucular kullanarak oluştururken, inşa öğe ve öznitelik adları ad alanı önekini kullanarak nitelikli. Önek ad alanına aşağıdaki şekillerde bağlanabilirsiniz:

  • Bir ad alanı bildirimi özniteliğini kullanarak.

  • xmlnamespaces WITH yan tümcesi kullanarak.

  • XQuery giriş.

Namespaces eklemek için bir Namespace bildirimi özniteliğini kullanma

Aşağıdaki örnek bir namespace declaration özniteliği öğesi yapımında kullandığı <a> varsayılan bir ad alanı bildirmek için. Alt öğe inşaat <b> üst öğesinde bildirilen varsayılan ad alanı bildirimi alır.

declare @x xml
set @x ='<x>5</x>'
select @x.query( '
  <a xmlns="a">
    <b />
  </a>' ) 

declare @x xml
set @x ='<x>5</x>'
select @x.query( '
  <a xmlns="a">
    <b />
  </a>' ) 

Sonuç şudur:

<a xmlns="a">
  <b  />
</a>

<a xmlns="a">
  <b  />
</a>

Bir önek için ad alanı atayabilirsiniz. Öneki öğe yapımında belirtilen <a>.

declare @x xml
set @x ='<x>5</x>'
select @x.query( '
  <x:a xmlns:x="a">
    <b/>
  </x:a>' )

declare @x xml
set @x ='<x>5</x>'
select @x.query( '
  <x:a xmlns:x="a">
    <b/>
  </x:a>' )

Sonuç şudur:

<x:a xmlns:x="a">
  <b />
</x:a>

<x:a xmlns:x="a">
  <b />
</x:a>

İle başlayan SQL Server 2005, un olabilir-xml inşaat varsayılan bir ad alanı bildirimini, ancak BM değil-bir ad alanı öneki ilan. BM olamaz çünkü aşağıdaki sorgu hata verir-eleman inşaatı'nda belirtilen bir önek bildirmek <b>.

declare @x xml
set @x ='<x>5</x>'
select @x.query( '
  <x:a xmlns:x="a">
    <b xmlns:x=""/>
  </x:a>' )

declare @x xml
set @x ='<x>5</x>'
select @x.query( '
  <x:a xmlns:x="a">
    <b xmlns:x=""/>
  </x:a>' )

Yeni oluşturulmuş ad sorgusu içinde kullanmak kullanılabilir. Örneğin, aşağıdaki sorgu öğe oluştururken ad bildirir <FirstLocation>ve ifadeleri LocationID ve SetupHrs öznitelik değerleri için önek belirtir.

SELECT Instructions.query('
        <FirstLocation xmlns:AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions"
         LocationID="{ (/AWMI:root/AWMI:Location[1]/@LocationID)[1] }"
         SetupHrs = "{ (/AWMI:root/AWMI:Location[1]/@SetupHours)[1] }" >
           { /AWMI:root/AWMI:Location[1]/AWMI:step }
        </FirstLocation> 
') as Result 
FROM  Production.ProductModel
where ProductModelID=7

SELECT Instructions.query('
        <FirstLocation xmlns:AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions"
         LocationID="{ (/AWMI:root/AWMI:Location[1]/@LocationID)[1] }"
         SetupHrs = "{ (/AWMI:root/AWMI:Location[1]/@SetupHours)[1] }" >
           { /AWMI:root/AWMI:Location[1]/AWMI:step }
        </FirstLocation> 
') as Result 
FROM  Production.ProductModel
where ProductModelID=7

Not Bu şekilde yeni bir ad alanı öneki oluşturma Bu önek için önceden varolan herhangi bir ad alanı bildirimi geçersiz kılar. Örneğin, ad alanı bildirimi, AWMI="http://someURI", sorguda prolog tarafından ad alanı bildiriminde geçersiz olur <FirstLocation>öğesi.

SELECT Instructions.query('
declare namespace AWMI="http://someURI";
        <FirstLocation xmlns:AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions"
         LocationID="{ (/AWMI:root/AWMI:Location[1]/@LocationID)[1] }"
         SetupHrs = "{ (/AWMI:root/AWMI:Location[1]/@SetupHours)[1] }" >
           { /AWMI:root/AWMI:Location[1]/AWMI:step }
        </FirstLocation> 
') as Result 
FROM  Production.ProductModel
where ProductModelID=7

SELECT Instructions.query('
declare namespace AWMI="http://someURI";
        <FirstLocation xmlns:AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions"
         LocationID="{ (/AWMI:root/AWMI:Location[1]/@LocationID)[1] }"
         SetupHrs = "{ (/AWMI:root/AWMI:Location[1]/@SetupHours)[1] }" >
           { /AWMI:root/AWMI:Location[1]/AWMI:step }
        </FirstLocation> 
') as Result 
FROM  Production.ProductModel
where ProductModelID=7

Namespaces eklemek için bir giriş kullanma

Bu örnekte, nasıl yapılandırılmış xml ad alanları eklenebilir gösterilmektedir. Varsayılan bir ad alanı sorgu giriş ilan edilir.

declare @x xml
set @x ='<x>5</x>'
select @x.query( '
           declare default element namespace "a";
            <a><b /></a>' )

declare @x xml
set @x ='<x>5</x>'
select @x.query( '
           declare default element namespace "a";
            <a><b /></a>' )

Öğe yapımında dikkat <b>, ad alanı bildirimi özniteliği değeri olarak boş bir dize ile belirtilir. Bu un-üst bildirilmiş varsayılan ad alanı ilan etti.

This is the result:
<a xmlns="a">
  <b  />
</a>

This is the result:
<a xmlns="a">
  <b  />
</a>

xml yapım ve boşluk işleme

Öğe içeriği xml yapım boşluk karakterleri içerebilir. Bu karakterler aşağıdaki şekilde işlenir:

  • Ad alanı URI boşluk karakterleri xsd türü olarak kabul edilir anyURI. Özellikle, nasıl işlenir budur:

    • Başında ve sonunda herhangi bir beyaz boşluk karakter kırpılır.

    • İç boşluk karakteri değerleri tek bir alana daraltılmış

  • Öznitelik içerik içinde satır besleme karakterleri boşlukla değiştirilir. Tüm boşluk karakterleri, oldukları gibi kalır.

  • Boşluk içindeki öğeler korunur.

Aşağıdaki örnekte, xml yapım boşluk işlemede gösterilmektedir:

-- line feed is repaced by space.
declare @x xml
set @x=''
select @x.query('

declare namespace myNS="   http://     
 abc/
xyz

";
<test attr="    my 
test   attr 
value    " >

<a>

This     is  a

test

</a>
</test>
') as XML_Result 

 
-- line feed is repaced by space.
declare @x xml
set @x=''
select @x.query('

declare namespace myNS="   http://     
 abc/
xyz

";
<test attr="    my 
test   attr 
value    " >

<a>

This     is  a

test

</a>
</test>
') as XML_Result 

 

Sonuç şudur:

-- result
<test attr="<test attr="    my test   attr  value    "><a>

This     is  a

test

</a></test>
"><a>

This     is  a

test

</a></test>

-- result
<test attr="<test attr="    my test   attr  value    "><a>

This     is  a

test

</a></test>
"><a>

This     is  a

test

</a></test>

Diğer doğrudan xml oluşturucular

İşlem yönergeleri ve xml açıklamaları için Kurucular, ilgili xml yapı aynı sözdizimini kullanır. Kurucular metin düğümleri de desteklenir, ancak öncelikle xml dml içinde metin düğümleri oluşturmak için kullanılan hesaplanmış.

Not bir açık metin düğümü Oluşturucu kullanarak örnek için bkz: Belirli örnekte (xml dml) Ekle.

Aşağıdaki sorgu, yapılandırılmış xml öğe, iki öznitelik, yorum ve işleme talimatı içerir. Önce virgül kullanılan Not <FirstLocation>, çünkü bir sıra oluşturulur.

SELECT Instructions.query('
  declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
   <?myProcessingInstr abc="value" ?>, 
   <FirstLocation 
        WorkCtrID = "{ (/AWMI:root/AWMI:Location[1]/@LocationID)[1] }"
        SetupHrs = "{ (/AWMI:root/AWMI:Location[1]/@SetupHours)[1] }" >
       <!-- some comment -->
       <?myPI some processing instructions ?>
       { (/AWMI:root/AWMI:Location[1]/AWMI:step) }
    </FirstLocation> 
') as Result 
FROM Production.ProductModel
where ProductModelID=7

SELECT Instructions.query('
  declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
   <?myProcessingInstr abc="value" ?>, 
   <FirstLocation 
        WorkCtrID = "{ (/AWMI:root/AWMI:Location[1]/@LocationID)[1] }"
        SetupHrs = "{ (/AWMI:root/AWMI:Location[1]/@SetupHours)[1] }" >
       <!-- some comment -->
       <?myPI some processing instructions ?>
       { (/AWMI:root/AWMI:Location[1]/AWMI:step) }
    </FirstLocation> 
') as Result 
FROM Production.ProductModel
where ProductModelID=7

Bu kısmi bir sonucudur:

<?myProcessingInstr abc="value" ?>
<FirstLocation WorkCtrID="10" SetupHrs="0.5">
  <!-- some comment -->
  <?myPI some processing instructions ?>
  <AWMI:step xmlns:AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions">I
  nsert <AWMI:material>aluminum sheet MS-2341</AWMI:material> into the <AWMI:tool>T-85A framing tool</AWMI:tool>. 
  </AWMI:step>
    ...
/FirstLocation>

<?myProcessingInstr abc="value" ?>
<FirstLocation WorkCtrID="10" SetupHrs="0.5">
  <!-- some comment -->
  <?myPI some processing instructions ?>
  <AWMI:step xmlns:AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions">I
  nsert <AWMI:material>aluminum sheet MS-2341</AWMI:material> into the <AWMI:tool>T-85A framing tool</AWMI:tool>. 
  </AWMI:step>
    ...
/FirstLocation>

Hesaplanan oluşturucular kullanma

. Bu durumda, oluşturmak istediğiniz düğümün türünü tanımlayan anahtar sözcükleri belirtin. Yalnızca aşağıdaki anahtar sözcükler desteklenir:

  • öğe

  • Öznitelik

  • metin

Öğe ve öznitelik düğümlerinin, bu anahtar düğümü adından ve ayrıca içine aşağıdaki ifade, o düğümün içeriğini oluşturan. Aşağıdaki örnekte, bu xml yapılandırırlar:

<root>
  <ProductModel PID="5">Some text <summary>Some Summary</summary></ProductModel>
</root>

<root>
  <ProductModel PID="5">Some text <summary>Some Summary</summary></ProductModel>
</root>

Bu kullanır oluşturucular xml oluşturmak hesaplanan sorgudur:

declare @x xml
set @x=''
select @x.query('element root 
               { 
                  element ProductModel
     {
attribute PID { 5 },
text{"Some text "},
    element summary { "Some Summary" }
 }
               } ')

declare @x xml
set @x=''
select @x.query('element root 
               { 
                  element ProductModel
     {
attribute PID { 5 },
text{"Some text "},
    element summary { "Some Summary" }
 }
               } ')

Düğüm içeriği oluşturan ifade bir sorgu ifadesinde belirtebilirsiniz.

declare @x xml
set @x='<a attr="5"><b>some summary</b></a>'
select @x.query('element root 
               { 
                  element ProductModel
     {
attribute PID { /a/@attr },
text{"Some text "},
    element summary { /a/b }
 }
               } ')

declare @x xml
set @x='<a attr="5"><b>some summary</b></a>'
select @x.query('element root 
               { 
                  element ProductModel
     {
attribute PID { /a/@attr },
text{"Some text "},
    element summary { /a/b }
 }
               } ')

Hesaplanan öğe ve öznitelik oluşturucular, XQuery belirtiminde tanımlanan düğüm adları hesaplamak için izin unutmayın. Doğrudan kurucuya kullandığınızda SQL Server, eleman ve öznitelik gibi düğüm adları sabit harflerin belirtilmelidir. Bu nedenle, doğrudan oluşturucular ve hesaplanan Kurucular öğeler ve öznitelikler için fark yoktur.

Aşağıdaki örnekte, inşa düğümleri için içerik yönergeleri sütunda depolanan xml üretim yönergeleri elde edilir xmlveri türü ProductModel tablosundaki.

SELECT Instructions.query('
  declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
   element FirstLocation 
     {
        attribute LocationID { (/AWMI:root/AWMI:Location[1]/@LocationID)[1] },
        element   AllTheSteps { /AWMI:root/AWMI:Location[1]/AWMI:step }
     }
') as Result 
FROM  Production.ProductModel
where ProductModelID=7

SELECT Instructions.query('
  declare namespace AWMI="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions";
   element FirstLocation 
     {
        attribute LocationID { (/AWMI:root/AWMI:Location[1]/@LocationID)[1] },
        element   AllTheSteps { /AWMI:root/AWMI:Location[1]/AWMI:step }
     }
') as Result 
FROM  Production.ProductModel
where ProductModelID=7

Bu kısmi bir sonucudur:

<FirstLocation LocationID="10">
  <AllTheSteps>
    <AWMI:step> ... </AWMI:step>
    <AWMI:step> ... </AWMI:step>
    ...
  </AllTheSteps>
</FirstLocation>  
<FirstLocation LocationID="10">
  <AllTheSteps>
    <AWMI:step> ... </AWMI:step>
    <AWMI:step> ... </AWMI:step>
    ...
  </AllTheSteps>
</FirstLocation>  

Ek uygulama kısıtlamaları

Hesaplanmış öznitelik oluşturucular, yeni bir ad alanı bildirmek için kullanılamaz. Ayrıca, aşağıdaki oluşturucular desteklenmez hesaplanan SQL Server:

  • Hesaplanan belge düğümü oluşturucular

  • Hesaplanan işleme yönergesi oluşturucular

  • Hesaplanan yorum oluşturucular

Ayrıca bkz.

Kavramlar

XQuery ifadeleri