Obtaining Namespace Information

You can call functions to obtain namespace information. This topic discusses

In addition to a discussion of the functions you call, this section covers the following:

Obtaining the Namespace URI

To obtain the URI for a namespace, call the namespace-uri() function. The format is

string namespace-uri(node-set?) 
               

            

The namespace-uri() function returns the namespace URI of the expanded name of the node in the node-set argument that is first in document order. If the node-set argument is empty, the first node has no expanded name, or the namespace URI of the expanded name is null, the XPath processor returns an empty string. If you omit the argument, it defaults to a node set with the context node as its only member.

Call the namespace-uri() function on element or attribute nodes. For example, the query

/bookstore/my:book/namespace-uri()
               

            

returns the string


              "http://www.placeholder-name-here.com/schema/"
               

            

For any other type of node, the XPath processor always returns an empty string.

Obtaining the Local Name

To obtain the local portion of a node name, excluding the prefix, call the local-name() function. The format is

string local-name(node-set?) 
               

            

The local-name() function returns the local part of the expanded name of the node in the node-set argument that is first in document order. If the node-set argument is empty or the first node has no expanded name, the function returns an empty string. If you omit the argument, it defaults to a node set with the context node as its only member. For example, the following query returns my:book nodes:

/bookstore/my:*[local-name()="book"]
               

            

Obtaining the Expanded Name

To obtain the expanded name of a node, call the name() function. The expanded name is the namespace prefix, if any, plus the local name. The format is

string name(node-set?) 
               

            

The name() function returns a string that represents the expanded name of the node in the node-set argument that is first in document order. The returned string represents the expanded name with respect to the namespace declarations in effect on the node whose expanded name is being represented.

Typically, this is the name in the XML source. This need not be the case if there are namespace declarations in effect on the node that associate multiple prefixes with the same namespace.

If the node-set argument is empty or the first node has no expanded name, the XPath processor returns an empty string. If you omit the argument, it defaults to a node set with the context node as its only member.

Except for element and attribute nodes, the string that the name() function returns is the same as the string the local-name() function returns.

Specifying Wildcards with Namespaces

Element and attribute names that include colons ( :) can include wildcards; that is, asterisks ( *). For example, queries can include *:*, *:a, or a:*.

Examples of Namespaces in Queries

The following example finds all book elements in the current context. This query does not return any book elements that are not in the default namespace. For example, it does not return my:book elements.

book 
               

            

The next query finds all book elements with the prefix my. This query does not return unqualified book elements; that is, book elements in the default namespace.

my:book 
               

            

The following query finds all book elements with a my prefix that have an author subelement:

my:book[author] 
               

            

The following query finds all book elements with a my prefix that have an author subelement with a my prefix:

my:book[my:author] 
               

            

The next example returns the style attribute with a my prefix for book elements in the current context:

book/@my:style 
               

            

 
Free Stylus Studio XML Training: