Home > Articles > Web Services > XML

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

3.4 Navigation Context

Every XQuery expression is evaluated within a context, and several of the context effects have a bearing on navigation. The context can vary during the evaluation of a path, and some context information can be accessed using functions or other expressions. XPath 1.0 defines six expression context information items, and XQuery adds nine more, all listed in Table 3.3.

Expression context is divided into static context, which is available during the compilation of the query, and evaluation context, which is available while the expression is being evaluated dynamically. Some context information is global to an entire query, while other context information is local and may vary during compilation or evaluation.

3.4.1 Input Sequence

One value in the evaluation context is the input sequence. This sequence can be accessed using the input() function, and doesn't change during the execution of a query. This value defines the initial context sequence (for example, used by relative paths at the top of the query) and may be empty.

3.4.2 Focus

Part of the XQuery evaluation context is called the focus. Predicates and navigation steps change the focus. This focus consists of three items: the context item, the context position, and the context size.

Table 3.3. XQuery expression context

Context item

Accessed with

Static or dynamic

XPath 1.0?

in-scope namespaces

get--in-scope-prefixes()

static

Yes

default element namespace

N/A

static

No

default function namespace

N/A

static

No

in-scope schema definitions

N/A

static

No

in-scope functions

N/A

static

Yes

in-scope collations

N/A

static

No

default collation

default-collation()

static

No

base-uri

base-uri()

static

No

in-scope variables

$variable

both

Yes

context item

.

dynamic

Yes

context position

position()

dynamic

Yes

context size

last()

dynamic

Yes

current date and time

current-date()

current-time()

current-dateTime()

dynamic

No

implicit timezone

implicit-timezone()

dynamic

No

input sequence

N/A

dynamic

No

When evaluating a path, the focus changes with each step and predicate. For example, when evaluating the path x[@y=1]/z, the step x selects a sequence of nodes, which defines the focus for the predicate. The context size is the number of nodes selected by x, and then for each node in that sequence, the predicate is evaluated. The node becomes the current context item, and the context position is its position within that sequence. If the predicate evaluates to true, then the node is kept in the result, otherwise it is omitted. The result of this step becomes the focus for the next step z.

The current context item can be accessed using the dot (.) expression, and in fact x[@y=1] is short for x[./@y = 1]. Every relative path in a predicate begins at the current context item.

The current context position can be accessed using the function position(). For example, when evaluating the path x[position() > 3], the predicate eliminates the first three items in the sequence selected by x.

Finally, the context size can be accessed using the function last(). For example, the path x[last()] selects the last child element named x. The efficiency of the last() function depends on the implementation. In cases where you are streaming through an XML input, last() always requires at least a little buffering to evaluate, and can require a lot of buffering. For example, x[count(y) < last()]must first count the number of x child elements, and then iterate through each of them testing the condition. (Implementations that preload the XML into memory or a database are less affected by this consideration, because they may already have the sequence length available.)

3.4.3 Variable Declarations

XQuery can also declare and use variables. Certain expressions, such as FLWOR and typeswitch, introduce new variables into scope. Some implementations also allow externally defined variables to be passed to an XQuery. You'll see examples of both of these later.

There are two aspects to variable context. In the static context are all the variable declarations, that is, the names and static types of the variables that are available to the XQuery expression. The evaluation context also contains this information, along with the variable values (and their dynamic types), called the variable bindings.

Variables are accessed by name using an expression such as $variable. Attempting to use a variable that isn't in the static context (that is, not in scope) causes a compile-time error.

3.4.4 Namespace Declarations

The static context also includes namespace declarations, which may be defined in the query prolog or in element constructors. The namespace declarations are just a set of prefix and namespace pairs that allow prefixes to be used to stand in for the namespace names. XQuery allows for two different kinds of default namespaces, one for resolving element and type names, and the other for resolving function names (see Chapter 5 for additional details about the query prolog).

3.4.5 Function Declarations

The static context also includes all functions available to the query. These include the built-in XQuery functions, as well as user-defined functions (see Chapter 4) and possibly other extension functions provided by the implementation (see Chapter 14).

XSLT 1.0 provides a function-available() function for determining whether a function is in the static context, but XQuery doesn't have an equivalent.

3.4.6 Collations

Collations are used for string comparisons and sorting; the default collation and possibly other in-scope collations are part of the static context. See Chapter 8 for details.

  • + Share This
  • 🔖 Save To Your Account