Stylus Studio XML Editor

Table of contents

Appendices

5.9 Expressions

Expressions

All property value specifications in attributes within an XSL stylesheet can be expressions. These expressions represent the value of the property specified. The expression is first evaluated and then the resultant value is used to determine the value of the property.

Property Context[top]

Property Context

Properties are evaluated against a property-specific context. This context provides:

  • A list of allowed resultant types for a property value.

  • Conversions from resultant expression value types to an allowed type for the property.

  • The current font-size value.

  • Conversions from relative numerics by type to absolute numerics within additive expressions.

NOTE: 

It is not necessary that a conversion is provided for all types. If no conversion is specified, it is an error.

When a type instance (e.g., a string, a keyword, a numeric, etc.) is recognized in the expression it is evaluated against the property context. This provides the ability for specific values to be converted with the property context's specific algorithms or conversions for use in the evaluation of the expression as a whole.

For example, the "auto" enumeration token for certain properties is a calculated value. Such a token would be converted into a specific type instance via an algorithm specified in the property definition. In such a case the resulting value might be an absolute length specifying the width of some aspect of the formatting object.

In addition, this allows certain types like relative numerics to be resolved into absolute numerics prior to mathematical operations.

All property contexts allow conversions as specified in [expr.value.conv] .

Evaluation Order[top]

Evaluation Order

When a set of properties is being evaluated for a specific formatting object in the formatting object tree there is a specific order in which properties must be evaluated. Essentially, the "font-size" property must be evaluated first before all other properties. Once the "font-size" property has been evaluated, all other properties may be evaluated in any order.

When the "font-size" property is evaluated, the current font-size for use in evaluation is the font-size of the parent element. Once the "font-size" property has been evaluated, that value is used as the current font-size for all property contexts of all properties value expressions being further evaluated.

Basics[top]

Basics
[1]   Expr   ::=    [AdditiveExpr]
[2]   PrimaryExpr   ::=   '(' [Expr] ')'
| [Numeric]
| [Literal]
| [Color]
| [Keyword]
| [EnumerationToken]
| [FunctionCall]

Function Calls[top]

Function Calls
[3]   FunctionCall   ::=    [FunctionName] '(' ( [Argument] ( ',' [Argument] )*)? ')'
[4]   Argument   ::=    [Expr]

Numerics[top]

Numerics

A numeric represents all the types of numbers in an XSL expression. Some of these numbers are absolute values. Others are relative to some other set of values. All of these values use a floating-point number to represent the number-part of their definition.

A floating-point number can have any double-precision 64-bit format IEEE 754 value [IEEE754] . These include a special "Not-a-Number" (NaN) value, positive and negative infinity, and positive and negative zero. See Section 4.2.3 of [JLS] for a summary of the key rules of the IEEE 754 standard.

[5]   Numeric   ::=    [AbsoluteNumeric]
| [RelativeNumeric]
[6]   AbsoluteNumeric   ::=    [AbsoluteLength]
[7]   AbsoluteLength   ::=    [Number] [AbsoluteUnitName] ?
[8]   RelativeNumeric   ::=    [Percent]
| [RelativeLength]
[9]   Percent   ::=    [Number] '%'
[10]   RelativeLength   ::=    [Number] [RelativeUnitName]

The following operators may be used with numerics:

+

Performs addition.

-

Performs subtraction or negation.

*

Performs multiplication.

div

Performs floating-point division according to IEEE 754.

mod

Returns the remainder from a truncating division.

NOTE: 

Since XML allows - in names, the - operator (when not used as a UnaryExpr negation) typically needs to be preceded by white space. For example the expression 10pt - 2pt means subtract 2 points from 10 points. The expression 10pt-2pt would mean a length value of 10 with a unit of "pt-2pt".

NOTE: 

The following are examples of the mod operator:

  • 5 mod 2 returns 1

  • 5 mod -2 returns 1

  • -5 mod 2 returns -1

  • -5 mod -2 returns -1

NOTE: 

The mod operator is the same as the % operator in Java and ECMAScript and is not the same as the IEEE remainder operation, which returns the remainder from a rounding division.

Numeric Expressions
[11]   AdditiveExpr   ::=    [MultiplicativeExpr]
| [AdditiveExpr] '+' [MultiplicativeExpr]
| [AdditiveExpr] '-' [MultiplicativeExpr]
[12]   MultiplicativeExpr   ::=    [UnaryExpr]
| [MultiplicativeExpr] [MultiplyOperator] [UnaryExpr]
| [MultiplicativeExpr] 'div' [UnaryExpr]
| [MultiplicativeExpr] 'mod' [UnaryExpr]
[13]   UnaryExpr   ::=    [PrimaryExpr]
| '-' [UnaryExpr]
NOTE: 

The effect of this grammar is that the order of precedence is (lowest precedence first):

  • +, -

  • *, div, mod

and the operators are all left associative. For example, 2*3 + 4 div 5 is equivalent to (2*3) + (4 div 5).

If a non-numeric value is used in an [AdditiveExpr] and there is no property context conversion from that type into an absolute numeric value, the expression is invalid and considered an error.

Absolute Numerics[top]

Absolute Numerics

An absolute numeric is an absolute length which is a pair consisting of a [Number] and a [UnitName] raised to a power. When an absolute length is written without a unit, the unit power is assumed to be zero. Hence, all floating point numbers are a length with a power of zero.

Each unit name has associated with it an internal ratio to some common internal unit of measure (e.g., a meter). When a value is written in a property expression, it is first converted to the internal unit of measure and then mathematical operations are performed.

In addition, only the mod, addition, and subtraction operators require that the numerics on either side of the operation be absolute numerics of the same unit power. For other operations, the unit powers may be different and the result should be mathematically consistent as with the handling of powers in algebra.

A property definition may constrain an absolute length to a particular power. For example, when specifying font-size, the value is expected to be of power "one". That is, it is expected to have a single powered unit specified (e.g., 10pt).

When the final value of a property is calculated, the resulting power of the absolute numeric must be either zero or one. If any other power is specified, the value is an error.

Relative Numerics[top]

Relative Numerics

Relative lengths are values that are calculated relative to some other set of values. When written as part of an expression, they are either converted via the property context into an absolute numeric or passed verbatim as the property value.

It is an error if the property context has no available conversion for the relative numeric and a conversion is required for expression evaluation (e.g., within an add operation).

Percents[top]

Percents

Percentages are values that are counted in 1/100 units. That is, 10% as a percentage value is 0.10 as a floating point number. When converting to an absolute numeric, the percentage is defined in the property definition as being a percentage of some known property value. If the percentage evaluates to "auto" the complete expression evaluates to "auto".

For example, a value of "110%" on a "font-size" property would be evaluated to mean 1.1 times the current font size. Such a definition of the allowed conversion for percentages is specified on the property definition. If no conversion is specified, the resulting value is a percentage.

Relative Lengths[top]

Relative Lengths

A relative length is a unit-based value that is measured against the current value of the font-size property.

There is only one relative unit of measure, the "em". The definition of "1em" is equal to the current font size. For example, a value of "1.25em" is 1.25 times the current font size.

When an em measurement is used in an expression, it is converted according to the font-size value of the current property's context. The result of the expression is an absolute length. See [font-size] .

Strings[top]

Strings

Strings are represented either as [literals] or as an [enumeration token] . All properties contexts allow conversion from enumeration tokens to strings. See [expr.value.conv] .

Colors[top]

Colors

A color is a set of values used to identify a particular color from a color space. Only RGB [sRGB] (Red, Green, Blue) and ICC (International Color Consortium) [ICC] colors are included in this Recommendation.

RGB colors are directly represented in the expression language using a hexadecimal notation. ICC colors can be specified through an rgb-icc function. Colors can also be specified through the system-color function or through conversion from an [EnumerationToken] via the property context.

Keywords[top]

Keywords

Keywords are special tokens in the grammar that provide access to calculated values or other property values. The allowed keywords are defined in the following subsections.

inherit[top]

inherit

The property takes the same computed value as the property for the formatting object's parent object.

NOTE: 

"inherit" is not allowed as an expression mixed with operations. The same functionality is provided by the from-parent() function, which can be mixed with operations.

Lexical Structure[top]

Lexical Structure

When processing an expression, white space ( [ExprWhitespace] ) may be allowed before or after any expression token even though it is not explicitly defined as such in the grammar. In some cases, white space is necessary to make tokens in the grammar lexically distinct. Essentially, white space should be treated as if it does not exist after tokenization of the expression has occurred.

The following special tokenization rules must be applied in the order specified to disambiguate the grammar:

Expression Lexical Structure
[14]   ExprToken   ::=   '(' | ')' | '%'
| [Operator]
| [FunctionName]
| [EnumerationToken]
| [Number]
[15]   Number   ::=    [FloatingPointNumber]
[16]   FloatingPointNumber   ::=    [Digits] ('.' [Digits] ?)?
| '.' [Digits]
[17]   Digits   ::=   [0-9]+
[18]   Color   ::=   '#' [AlphaOrDigits]
[19]   AlphaOrDigits   ::=   [a-fA-F0-9]+
[20]   Literal   ::=   '"' [^"]* '"'
| "'" [^']* "'"
[21]   Operator   ::=    [OperatorName]
| [MultiplyOperator]
| '+' | '-'
[22]   OperatorName   ::=   'mod' | 'div'
[23]   MultiplyOperator   ::=   '*'
[24]   Keyword   ::=   'inherit'
[25]   FunctionName   ::=    NCName
[26]   EnumerationToken   ::=   NCName
[27]   AbsoluteUnitName   ::=   'cm' | 'mm' | 'in' | 'pt' | 'pc' | 'px'
[28]   RelativeUnitName   ::=   'em'
[29]   ExprWhitespace   ::=   S

Expression Value Conversions[top]

Expression Value Conversions

Values that are the result of an expression evaluation may be converted into property value types. In some instances this is a simple verification of set membership (e.g., is the value a legal country code). In other cases, the value is expected to be a simple type like an integer and must be converted.

It is not necessary that all types be allowed to be converted. If the expression value cannot be converted to the necessary type for the property value, it is an error.

The following table indicates what conversions are allowed.

1 lefttopTypeAllowed ConversionsConstraintslefttop lefttoplefttop
11lefttopNCName 11lefttop
  • Color, via the system-color() function.

  • Enumeration value, as defined in the property definition.

  • To a string literal

11lefttopThe value may be checked against a legal set of values depending on the property.
11lefttop [AbsoluteNumeric] 11lefttop
  • Integer, via the round() function.

  • Color, as an RGB color value.

11lefttop If converting to an RGB color value, it must be a legal color value from the color space.
11lefttop [RelativeLength] 11lefttop
  • To an AbsoluteLength

11lefttop

The specific conversion to be applied is property specific and can be found in the definition of each property.

NOTE: 

Conversions of compound property values are not allowed; thus for example, space-before.optimum="inherited-property-value(space-before)" is invalid. Permitted are, for example, space-before="inherited-property-value(space-before)" and space-before.optimum="inherited-property-value(space-before.optimum)" since they do not require conversion.

Definitions of Units of Measure[top]

Definitions of Units of Measure

The units of measure in this Recommendation have the following definitions:

1 lefttopNameDefinitionlefttoplefttoplefttoplefttoplefttoplefttoplefttop
11lefttopcm11lefttopSee [ISO31]
11lefttopmm11lefttopSee [ISO31]
11lefttopin11lefttop2.54cm
11lefttoppt11lefttop1/72in
11lefttoppc11lefttop12pt
11lefttoppx11lefttopSee [pixels]
11lefttopem11lefttopSee [relative.lengths]

Pixels[top]

Pixels

XSL interprets a 'px' unit to be a request for the formatter to choose a device-dependent measurement that approximates viewing one pixel on a typical computer monitor. This interpretation is follows:

  1. The preferred definition of one 'px' is:

    • The actual distance covered by the largest integer number of device dots (the size of a device dot is measured as the distance between dot centers) that spans a distance less-than-or-equal-to the distance specified by the arc-span rule in [http://www.w3.org/TR/REC-CSS2//syndata.html#x39] . In XSL this is assumed to be 0.28mm [approximately 1/90"] for print, desktop computer monitors, and hand-held devices viewed at normal viewing distances.

    • A minimum of the size of 1 device dot should be used.

    • This calculation is done separately in each axis, and may have a different value in each axis.

  2. However, implementors may instead simply pick a fixed conversion factor, treating 'px' as an absolute unit of measurement (such as 1/92" or 1/72").

NOTE: 

Pixels should not be mixed with other absolute units in expressions as they may cause undesirable effects. Also, particular caution should be used with inherited property values that may have been specified using pixels.

If the User Agent chooses a measurement for a 'px' that does not match an integer number of device dots in each axis it may produce undesirable effects, such as:

  • moir patterns in scaled raster graphics

  • unrenderable overlapping areas when the renderer rounds fonts or graphics sizes upward to its actual dot-size

  • large spaces between areas when the renderer rounds fonts or graphics sizes downward to its actual dot-size

  • unreadable results including unacceptably small text/layout (for example, a layout was done at 72 dpi [dots per inch], but the renderer assumed the result was already specified in device dots and renders it at 600 dpi).

Stylesheet authors should understand a pixel's actual size may vary from device to device:

  • stylesheets utilizing 'px' units may not produce consistent results across different implementations or different output devices from a single implementation

  • even if stylesheets are expressed entirely in 'px' units the results may vary on different devices