The SPS designer can define customized XPath 2.0/3.0 functions. A user-defined XPath function can be re-used in any design component that accepts an XPath expression, for example, in Auto-Calculations, conditions, and combo boxes.
User-defined XPath functions are created (and subsequently accessed for editing) in either the Schema Tree sidebar or the Design Tree sidebar (see screenshot below). All the user-defined XPath functions in an SPS are listed under the XPath Functions item in both the Schema Tree and Design Tree sidebars and can be accessed via either sidebar.
To create a user-defined XPath function, click the icon of the XPath Functions item. This pops up the XPath Functions dialog (screenshot below). If you wish to edit a function that has already been created, double-click its entry in the list of XPath functions. The XPath Functions dialog (screenshot below) will appear and the function definition can be edited.
After a user-defined XPath function is created, it is available for use anywhere in the design.
User-defined XPath functions are created in the namespace: http://www.altova.com/StyleVision/user-xpath-functions. This namespace is bound to the prefix sps:, so user-defined XPath functions must be called using this namespace prefix. For example, sps:MyFunction().
Each user-defined XPath function can be enabled or disabled by, respectively, checking or unchecking the check box to the left of the function's entry in the list of user-defined XPath functions (see screenshot below).
This feature is useful if two functions have the same name. Such a situation could arise, for example, when an imported SPS module contains a function having the same name.
A user-defined XPath function can be called in an XPath expression at any location in the design. For example, the user-defined XPath function sps:MyFunction defined above can be called, for example, with the following XPath expression in an Auto-Calculation:
This XPath expression would be evaluated as follows:
1.The sps:MyFunction() function is evaluated. Let's say the function is defined as follows: $XML/Trades/Stock[@name=$XML/Trades/Selection/Stock]. When the function is evaluated it returns the /Trades/Stock element that has a name attribute with a value that matches the content of the /Trades/Selection/Stock element.
2.The result of Step 1 is returned to the XPath expression in the function call. Now the value of the name attribute of this /Trades/Stock element is returned as the value of the Auto-Calculation.
To delete a function, select it in the XPath Functions list in the Schema Tree or Design Tree sidebar and then click the Remove Item icon in the toolbar of the sidebar. Alternatively, you can right-click the XPath function and select Remove Item from the context menu.