File name: txml_using_contexts.html![[AIX HP-UX Linux Solaris Windows]](../../dist.gif)

Using static and dynamic contexts
You can use the two context interfaces that the XML API
provides—XStaticContext and XDynamicContext.
About this task
- Static context
- The static context is used to configure prepare-time characteristics.
Note: Prepare-time
refers to the execution of one of the prepare methods on XFactory
or the execution of one of the compile methods on XCompilationFactory.
Static
context defines items that are needed to prepare executables, items
such as the names and types of external variables and functions that
will be available at run time as well as compilation modes like backwards
compatibility, math mode, and so on. These items do not change across
invocations.
- Dynamic context
- The dynamic context is used to configure execution-time characteristics.
Dynamic
context defines items that are unique to each invocation of an executable,
items such as the values for external variables, external function
implementations, and resolvers to external inputs or results. These
items might change across invocations.
Prepare-time characteristics are
not set directly on the XFactory instance so that it can be thread
safe. The same is true for execution-time characteristics; they are
kept in a separate object from the XExecutable instance so that the
executable object itself is thread safe.
The prepare and execute
steps themselves are separate because preparation takes time and it
would be inefficient to prepare for every execution. Having separate
steps allows an expression, query, or stylesheet to be prepared once
and the resulting executable object then can be used to process any
number of input documents.
Procedure
- Use the static context, XStaticContext, to configure prepare-time
characteristics.
Prepare-time characteristics are built
directly into the executable object; therefore, after an executable
object has been created with a certain set of characteristics, they
are fixed and cannot be changed. If an executable object for the
same expression, query, or stylesheet is required that has different
characteristics, a new one must be generated.
Examples
of methods on XStaticContext used for setting prepare-time characteristics:
- setUseCompiler
- The executable object that is generated is very different for
compiled and interpreted; therefore, this is a prepare-time characteristic.
- declareVariable
- The type of a variable affects how the expression, query, or stylesheet
gets compiled and therefore is a prepare-time characteristic.
On
the other hand, binding the value of a variable is an execution-time
characteristic, and the value can be different for each execution.
- setSourceResolver
- A source resolver registered at prepare time is used to resolve
includes and imports.
A source resolver can also be registered
at execution time, but it is used for a different purpose.
- Use the dynamic context, XDynamicContext, to configure
execution-time characteristics.
Execution-time characteristics
can be different on each call to the execute method of an executable
object.
Examples of methods on XDynamicContext
used for setting execution-time characteristics:
- bind methods
- The bind methods on XDynamicContext interface are used to supply
a value for a variable. The value can be different for each execution.
- setSourceResolver
- A source resolver registered at execution time is used to resolve
input documents loaded with the XPath fn:doc function or the XSLT
fn:document function.
- setXSLTInitialTemplate
- Identify the initial template to invoke for an XSL transformation.
What to do next
The XPath, XQuery, and XSLT specifications also have the
concepts of static and dynamic context. For more information, see
the following Web pages:
The XStaticContext and XDynamicContext interfaces merge
settings from all three languages (XPath, XQuery, and XSLT). To find
out which settings apply to which language refer to the "Performing
Basic Operations" article for that language in the related tasks listed
below.
|
