dize işlev (XQuery)
Değerini döndürür $arg temsil edilen gibi bir dize.
Sözdizimi
fn:string() as xs:string
fn:string($arg as item()?) as xs:string
Bağımsız değişkenler
- $arg
Bir düğüm veya atomik bir değer değil.
Açıklamalar
If $arg is the empty sequence, the zero-length string is returned.
If $arg is a node, the function returns the string value of the node that is obtained by using the string-value accessor.Bu XPath 2.0 veri modeli W3C XQuery 1.0 belirtiminde tanımlanmıştır.
If $arg is an atomic value, the function returns the same string that is returned by the expression cast as xs:string, $arg, except when noted otherwise.
Yoksa türü $arg olan xs:anyURI, URI dönüştürülür bir dize özel karakterler kaçış olmadan.
Bu uygulama fn:dize() olmayan bir bağımsız değişken yalnızca içerik bağımlı yüklemi içeriğinde kullanılabilir.Özellikle, bu yalnızca köşeli ayraçlar ([]) içinde kullanılabilir.
Örnekler
Bu konuda çeşitli içinde depolanan xml örnekleri karşı XQuery örnekler sağlar xml sütunları yazın AdventureWorks2008R2 veritabanı.Bir bakış bu sütunların her biri için bkz: XML veri türü temsili AdventureWorks2008R2 veritabanında.
A.Bir dize kullanarakişlev
Aşağıdaki sorgu alır <Features> alt öğe düğümü, <ProductDescription> öğesi.
SELECT CatalogDescription.query('
declare namespace PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
/PD:ProductDescription/PD:Features
')
FROM Production.ProductModel
WHERE ProductModelID=19
Bu kısmi bir sonucudur:
<PD:Features xmlns:PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription">
These are the product highlights.
<p1:Warranty xmlns:p1="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain">
<p1:WarrantyPeriod>3 years</p1:WarrantyPeriod>
<p1:Description>parts and labor</p1:Description>
</p1:Warranty>
...
</PD:Features>
Belirtirseniz, string() işlev, belirtilen düğüm dize değerini alır.
SELECT CatalogDescription.query('
declare namespace PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
string(/PD:ProductDescription[1]/PD:Features[1])
')
FROM Production.ProductModel
WHERE ProductModelID=19;
Bu kısmi bir sonucudur.
These are the product highlights.
3 years parts and labor...
B.Bir dize kullanarak işlev çeşitli düğümlerde
Aşağıdaki örnekte, bir xml türü değişkeni için bir xml örnek atanır.Sorguları uygulanması sonucunu göstermek için belirtilmiş dize() çeşitli düğümlerde.
declare @x xml;
set @x = '<?xml version="1.0" encoding="UTF-8" ?>
<!-- This is a comment -->
<root>
<a>10</a>
just text
<b attr="x">20</b>
</root>
';
Aşağıdaki sorgu, belge düğümü dize değerini alır.Bu değer tüm alt öğe metin düğümü dize değeri birleştirilmesiyle oluşturulmuş.
select @x.query('string(/)');
Bu sonucu verir:
This is a comment 10
just text
20
Bir işlem yönergesi düğümü dize değerini almak aşağıdaki sorgu çalışır.Bir metin düğümü içermediği için boş bir sıra sonucudur.
select @x.query('string(/processing-instruction()[1])');
Aşağıdaki sorgu açıklama düğüm dize değerini alır ve metin düğümü döndürür.
select @x.query('string(/comment()[1])');
Bu sonucu verir:
This is a comment