XPath Context Variables

Using context variables, you can create XPath expressions that reference values that may not be known at design time. For example, you wish to create an XPath into an element whose name comes from a message and is not known in advance. 
gateway83
Using context variables, you can create XPath expressions that reference values that may not be known at design time. For example, you wish to create an XPath into an element whose name comes from a message and is not known in advance. 
You may use context variables in the XPath expressions of the following assertions:
Encrypt Element
Evaluate Request Element
Evaluate Response Element
Sign Element
Require XPath Credentials
WARNING:
Context variables in XPath expressions are not supported in the 
Layer7 API Gateway
- XML VPN Client. This means that integrity or confidentiality assertions containing XPath expressions with context variables will work on the
Layer7 API Gateway
, but the assertions will fail on the 
Layer7 API Gateway
- XML VPN Client.
Types of XPath Expressions
There are two kinds of XPath expressions:
static
and
dynamic
.
  • A
    static
    expression is compiled once, and then the compiled form is used many times.  A static expression may refer to variables.  Any references to variable are added to the compiled form of the expression and their values are retrieved at runtime, for each invocation.  Static expressions use variables with the syntax
    $variable
    embedded in the expression. These variables can be used in place of a constant expression. For example, in the expression
    /foo[@bar="blah"]
    , you can replace "blah" with a variable like this:
    /foo[@bar=$variable]
  • A
    dynamic
    expression is compiled from scratch every time the expression is evaluated. This means its contents can be completely different each time it is invoked. To use a dynamic expression, the value typed into the XPath assertion dialog must start with "${" and end with "}", and contain a single context variable name in between. At runtime this expression is treated as a standard context variable, where the value is looked up, compiled as an XPath expression, and then evaluated for every invocation.
    (1) Fully dynamic XPath expressions are valid only in an XPath context. (2) Currently, only these two XPath assertions support fully dynamic XPath expression: Evaluate Response XPath.
Examples:
Statically-compiled expression that includes compiled-in variable reference:
Response must match XPath "/foo[@bar=$variable]"
Dynamically-constructed expression:
Set variable $xpath = "/foo[@bar=43]" Response must match XPath "${xpath}"
Dynamically-constructed expression that includes compiled-in variable reference:
Set variable $xpath = "/foo[@bar="${variable}"]" Response must match XPath "${xpath}"
Dynamically-constructed expression of the XPath expression "/book/author[last()]":
Set variable ${xpathVar} as String "/book/author[last()]" Evaluate Request XPath ${xpathVar}
Note:
The Evaluate Response XPath assertion may also be used. The variable "
${xpathVar}
" is entered into the "XPath" field in the assertion properties.
In general, context variables can be used wherever XPath function calls inside a predicate can be used. The exact rules dictating where a context variable may appear in an XPath expression is given by the XPath 1.0 grammar. For more information, see http://www.w3.org/TR/xpath/
Supported XPath Expressions
The following table lists the expressions that are supported.
Expression
Context Variable Value
Result of Evaluating Expression
$booleanContextVar
boolean
The Boolean value of
$booleanContextVar
(true/false), similar to evaluating expressions such as "1=1" or "1=0".
${stringContextVar}
string
The string value of
${stringContextVar}
, similar to evaluating an expression such as "Foo" (including the quotes).
${nodeContextVar}
DOM Node
A nodeset containing the single node contained in
${nodeContextVar}
, similar to matching it in the target document. The node does not need to be from the same document as the target message.
//*[local-name()=${stringContextVar}]
string
A nodeset containing all nodes in the target message with the local name equal to the value of
${stringContextVar}
.
/s:Envelope/s:Body/*[local-name()=$payloadLocalName and namespace-uri()=$payloadUri]
string, string
A nodeset containing the SOAP payload element(s).
This assumes the context variables
${payloadLocalName}
and
${payloadUri}
are set to the expected payload element local name and namespace URI respectively, and the target message contains the expected payload element(s).