Jaa


string Function (XQuery)

Returns the value of $arg represented as a string.

Syntax

fn:string() as xs:string
fn:string($arg as item()?) as xs:string

Arguments

  • $arg
    Is a node or an atomic value.

Remarks

  • 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. This is defined in the W3C XQuery 1.0 and XPath 2.0 Data Model specification.
  • 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.
  • If the type of $arg is xs:anyURI, the URI is converted to a string without escaping special characters.
  • Inthis implementation, fn:string() without an argument can only be used in the context of a context-dependent predicate. Specifically, it can only be used inside brackets ([ ]).

Examples

This topic provides XQuery examples against XML instances that are stored in various xml type columns in the AdventureWorks database. For an overview of each of these columns, see xml Data Type Representation in the AdventureWorks Database.

A. Using the string function

The following query retrieves the <Features> child element node of the <ProductDescription> element.

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

This is the partial result:

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

If you specify the string() function, you receive the string value of the specified node.

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

This is the partial result.

These are the product highlights. 
3 yearsparts and labor...  

B. Using the string function on various nodes

In the following example, an XML instance is assigned to an xml type variable. Queries are specified to illustrate the result of applying string() to various nodes.

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

The following query retrieves the string value of the document node. This value is formed by concatenating the string value of all its descendent text nodes.

select @x.query('string(/)')

This is the result:

This is a comment 10
just text
 20

The following query tries to retrieve the string value of a processing instruction node. The result is an empty sequence, because it does not contain a text node.

select @x.query('string(/processing-instruction()[1])')

The following query retrieves the string value of the comment node and returns the text node.

select @x.query('string(/comment()[1])')

This is the result:

This is a comment 

See Also

Reference

XQuery Functions Against the xml Data Type

Help and Information

Getting SQL Server 2005 Assistance