Altova StyleVision 2024 Basic Edition

Styles can be assigned to design components via XPath expressions. This enables style property values to be taken from XML data or from the XPath expression itself. Also, by using the doc() function of XPath 2.0/3.0, nodes in any accessible XML document can be addressed. Not only can style definitions be pulled from XML data; this feature also enables style choices to be made that are conditional upon the structure or content of the XML data. For example, using the if...else statement of XPath 2.0/3.0, two different background colors can be selected depending on the position of an element in a sequence. Thus, when these elements are presented as rows in a table, the odd-numbered rows can be presented with one background color while the even-numbered rows are presented with another (see below for example). Also, depending on the content of a node, the presentation can be varied.

 

Style properties for which XPath expressions are enabled

XPath expressions can be entered for the following style properties:

 

All properties available in the Styles sidebar

The Common, Event, and HTML groups of properties in the Properties sidebar

 

Static mode and dynamic (XPath) mode for property values

For those properties where XPath expressions are enabled, two mode are available:

 

Static mode, where the value of the property is entered directly in the Value column of the sidebar. For example, for the background-color of a design component, the value red can be entered directly in the sidebar.

Dynamic, or XPath mode, where an XPath expression is entered. The expression is evaluated at runtime, and the result is entered as the value of the property. For example, for the background color of a design component, the following XPath expression can be entered: /root/colors/color1. At runtime, the content of the node /root/colors/color1 will be retrieved and entered as the value of the background-color property.

 

Switching between static and dynamic (XPath) modes

For each property for which XPath expressions are enabled, static mode is selected by default. To switch a property to dynamic (XPath) mode, select that property and click the XPath icon in the toolbar of the sidebar (screenshot below).

 

EH_Styles_ViaXPath

 

If a static value was present for that property, it is now cleared and the mode is switched to dynamic. The Edit XPath Expression dialog appears. It is in this dialog that you enter the XPath expression for the property. Click OK when finished.

 

After you enter an XPath expression for the property, an Edit XPath expression button appears in the Value column for that property (screenshot above). Click this button to subsequently edit the XPath expression. If you wish to switch back to static mode, click the XPath icon in the toolbar. This will clear the XPath expression and switch the property to static mode.

 

Note:There are two important points to note. First, only one mode (static or dynamic), and the value/expression for that mode, is active at any time. Any value/expression that previously existed for the other mode is cleared; so switching to the other mode will present that mode with an empty entry field. (In order to go back to a previous value/expression, use the Undo command.) Second, if you reselect a property after further editing the SPS, then that property will be opened in the mode it was in previously.

 

Creating and editing the XPath definition

The XPath definition is created and edited in the Edit XPath Expression dialog. This dialog is accessed in two ways:

 

Each time you switch to the dynamic mode of a property from static mode (by clicking the XPath icon in the toolbar of the sidebar), the Edit XPath Expression dialog appears. You can now create the XPath expression. (Note that clicking the toolbar icon when already in dynamic mode switches the mode to static mode; it does not pop up the Edit XPath Expression dialog.)

The Edit XPath Expression dialog also pops up when you click the Edit XPath Expression button in the Value field of a property that already has an XPath expression defined for it. The dialog will contain the already defined XPath expression for that property, which you can now edit.

 

After you enter or edit the XPath expression in the entry field, click OK to finish.

 

Values returned by XPath expressions

The most important benefits of using XPath expressions to set a property value are that: (i) the property value can be taken from an XML file (instead of being directly entered); and/or (ii) an XPath expression can test some condition relating to the content or structure of the XML document being processed, and accordingly select a value. XPath expressions return values in the following two categories:

 

XML node content

The XPath expression can address nodes in: (i) the XML document being processed by the SPS, or (ii) any accessible XML document. For example the expression Format/@color would access the color attribute of the Format child of the context node. The value of the color attribute will be set as the value of the property for which the XPath expression was defined. A node in some other XML document can be accessed using the doc() function of XPath 2.0. For example, the expression doc('Styles.xml')//colors/color-3 would retrieve the value of the element color-3 in the XML document Styles.xml and set this value as the value of the property for which the XPath expression was defined.

 

XPath expression

The value of the property can come from the XPath expression itself, not from the XML document. For example, the background color of an element that is being output as a row can be made to alternate depending on whether the position of the row is odd-numbered or even-numbered. This could be  achieved using the XPath 2.0/3.0 expression: if (position() mod 2 = 0) then 'red' else 'green'. Note that the return value of this expression is either the string red or the string green, and it will be set as the value of the property for which the XPath expression was defined. In the example just cited, the property values were entered as string literals. Alternatively, they could come from an XML document, for example: if (position() mod 2 = 0) then doc('Styles.xml')//colors/color-1 else doc('Styles.xml')//colors/color-2. Conversely, the XPath expression could be a straightforward string, for example: 'green'. However, this is the same as entering the static value green for the property.

 

© 2017-2023 Altova GmbH