Altova StyleVision 2025 Professional Edition

User-Defined XPath Functions

Home Prev Top Next

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.

 

Defining and editing user-defined XPath functions

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.

 

XPFxOviewDesTree

 

To create a user-defined XPath function, click the Plus 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.

 

XPFxDlg01

 

After a user-defined XPath function is created, it is available for use anywhere in the design.

 

Namespace of user-defined XPath functions

User-defined XPath functions are created in the namespace: https://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().

 

Enabling and disabling user-defined XPath functions

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).

 

UserDefXPFxEnabling

 

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.

 

Calling a user-defined XPath function

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:

 

 sps:MyFunction()/@name.

 

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.

 

Deleting a function

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.

 

© 2018-2024 Altova GmbH