Home >
Online Product Documentation >
Table of Contents >
Obtaining Information About a Node or a Node Set
Obtaining Information About a Node or a Node Set
In a query, you can perform the following operations to obtain information about a node:
Obtaining the Name of a Node
The
name() function returns a string that contains the tag name of the node, including the namespace prefix, if any.
The following query returns the name of the third element in
bookstore, which is
"magazine".
Wildcards
An asterisk ( * ) specifies a wildcard name for element names. If there are comments before the third element in the preceding example, this query does not include them in the count. See
Filtering Results of Queries.
|
Note
|
|
Remember, an asterisk that is not preceded by an at sign (@) never returns attributes. The XPath processor does not include attributes in node counts. See
Attributes and Wildcards.
|
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
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
returns the string
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
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:
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
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.
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.
The following query finds all
book elements with a
my prefix that have an
author subelement:
The following query finds all
book elements with a
my prefix that have an
author subelement with a
my prefix:
The next example returns the
style attribute with a
my prefix for
book elements in the current context:
Obtaining the URI for an Unparsed Entity
To obtain the URI for an unparsed entity, call the
unparsed-entity-uri() function. The format is
This function returns the URI of the unparsed entity with the specified name that is in the same document as the context node. If there is no such entity, this function returns an empty string.
Determining the Number of Nodes in a Collection
To obtain the number of nodes in a node set, call the
count() function. The format is
The
count() function returns the number of nodes in the specified set. For example, the following query finds all authors who have more than ten publications:
To obtain the number of nodes in the current context, call the
last() function, described in the next section.
Determining the Context Size
To obtain the number of nodes in the current context, call the
last() function. The format is
The
last() function returns a number equal to the context size of the expression evaluation context. Essentially, the
last() function returns the position number of the last node in document order for the current context. For example, the following query returns all books if there are three or more of them. There are three
book elements in the current context. Consequently, this query returns three
book elements.