namespace-uri Function (XQuery)

Returns the namespace URI of the QName specified in $arg as a xs:string.

fn:namespace-uri() as xs:string
fn:namespace-uri($arg as node()?) as xs:string


Node name whose namespace URI part will be retrieved.

  • If the argument is omitted, the default is the context node.

  • In SQL Server, fn:namespace-uri() without an argument can only be used in the context of a context-dependent predicate. Specifically, it can only be used inside brackets ([ ]).

  • If $arg is the empty sequence, the zero-length string is returned.

  • If $arg is an element or attribute node whose expanded-QName is not in a namespace, the function returns the zero-length string

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

A. Retrieve namespace URI of a specific node

The following query is specified against an untyped XML instance. The query expression, namespace-uri(/ROOT[1]), retrieves the namespace URI part of the specified node.

set @x='<ROOT><a>111</a></ROOT>'
SELECT @x.query('namespace-uri(/ROOT[1])')

Because the specified QName does not have the namespace URI part but only the local name part, the result is a zero-length string.

The following query is specified against the Instructions typed xml column. The expression, namespace-uri(/AWMI:root[1]/AWMI:Location[1]), returns the namespace URI of the first <Location> element child of the <root> element.

SELECT Instructions.query('
declare namespace AWMI="" ;
     namespace-uri(/AWMI:root[1]/AWMI:Location[1])') as Result
FROM Production.ProductModel
WHERE ProductModelID=7;

This is the result:

B. Using namespace-uri() without argument in a predicate

The following query is specified against the CatalogDescription typed xml column. The expression returns all the element nodes whose namespace URI is The namespace-uri() function is specified without an argument and uses the context node.

SELECT CatalogDescription.query('
declare namespace p1="";
   /p1:ProductDescription//*[namespace-uri() = ""]
') as Result
FROM Production.ProductModel
WHERE ProductModelID=19l

This is partial result:

<p1:wheel xmlns:p1="">High performance wheels.</p1:wheel>

<p2:saddle xmlns:p2="">

<p3:i xmlns:p3="">Anatomic design</p3:i> and made from durable leather for a full-day of riding in comfort.</p2:saddle>

You can change the namespace URI in the previous query to You will then receive all the element node children of the <ProductDescription> element whose namespace URI part of the expanded QName is

Implementation Limitations

These are the limitations:

  • The namespace-uri() function returns instances of type xs:string instead of xs:anyURI.

Community Additions