Parameters and Nodes
When using parameters in XPath functions that locate nodes, it is important to bear in mind that the function has no context node, no matter from where in the design it is called. The context node can be supplied either in the XPath expression that is used to define the function (that is, in the Function Body pane) or in the XPath expression that is used to call the XPath function. In the latter case, the context can be supplied via arguments in the function call.
Consider the user-defined XPath function Stock(), which is defined with three parameters as shown in the screenshot below.
The definition in the function body is \$ContextStock[@name=\$Selection]/\$StockInfo, which uses the three parameters but contains no context node information. The context node information can be supplied in the XPath expression that calls the function, for example in this way:
sps:Stock( \$XML/Trades/Stock, \$XML/Trades/Selection/Stock, @name )
The function call has three arguments, the value of each of which supplies either context or node-locator information. Alternatively, the following XPath expressions can be used as the function-call and give the same results:
sps:Stock( /Trades/Stock, /Trades/Selection/Stock, @name )
sps:Stock( /Trades/Stock, //Selection/Stock, @name )
The \$XML variable, which returns the document root, can be left out in function calls from design components because in the XPath expressions of design components the context node is known.
Notice that in the function-call listed above there are three input arguments corresponding respectively to the three parameters defined for the user-defined XPath function:
• \$ContextStock = \$XML/Trades/Stock (the /Trades/Stock element)
• \$Selection = \$XML/Trades/Selection/Stock (the /Trades/Selection/Stock element)
• \$StockInfo = @name
The XPath expression in the function definition is:
When the input arguments are substituted, the XPath expression in the function definition becomes:
It is important to note that it is the nodesets that are passed to the function, not the text strings.
It is in this way that the context node and location steps are passed to the function via parameters. The function can then be evaluated to locate and return the required nodes.