Stylus Studio XML Editor

Table of contents

Appendices

4.2 String Functions

String Functions

string string(object?)

The string function converts an object to a string as follows:

  • A node-set is converted to a string by returning the [string-value] of the node in the node-set that is first in [document order] . If the node-set is empty, an empty string is returned.

  • A number is converted to a string as follows

    • NaN is converted to the string NaN

    • positive zero is converted to the string 0

    • negative zero is converted to the string 0

    • positive infinity is converted to the string Infinity

    • negative infinity is converted to the string -Infinity

    • if the number is an integer, the number is represented in decimal form as a [Number] with no decimal point and no leading zeros, preceded by a minus sign (-) if the number is negative

    • otherwise, the number is represented in decimal form as a [Number] including a decimal point with at least one digit before the decimal point and at least one digit after the decimal point, preceded by a minus sign (-) if the number is negative; there must be no leading zeros before the decimal point apart possibly from the one required digit immediately before the decimal point; beyond the one required digit after the decimal point there must be as many, but only as many, more digits as are needed to uniquely distinguish the number from all other IEEE 754 numeric values.

  • The boolean false value is converted to the string false. The boolean true value is converted to the string true.

  • An object of a type other than the four basic types is converted to a string in a way that is dependent on that type.

If the argument is omitted, it defaults to a node-set with the context node as its only member.

NOTE: 

The string function is not intended for converting numbers into strings for presentation to users. The format-number function and xsl:number element in XSLT provide this functionality.

string concat(string, string, string)

The concat function returns the concatenation of its arguments.

boolean starts-with(string, string)

The starts-with function returns true if the first argument string starts with the second argument string, and otherwise returns false.

boolean contains(string, string)

The contains function returns true if the first argument string contains the second argument string, and otherwise returns false.

string substring-before(string, string)

The substring-before function returns the substring of the first argument string that precedes the first occurrence of the second argument string in the first argument string, or the empty string if the first argument string does not contain the second argument string. For example, substring-before("1999/04/01","/") returns 1999.

string substring-after(string, string)

The substring-after function returns the substring of the first argument string that follows the first occurrence of the second argument string in the first argument string, or the empty string if the first argument string does not contain the second argument string. For example, substring-after("1999/04/01","/") returns 04/01, and substring-after("1999/04/01","19") returns 99/04/01.

string substring( string , number , number? )

The substring function returns the substring of the first argument starting at the position specified in the second argument with length specified in the third argument. For example, substring("12345",2,3) returns "234". If the third argument is not specified, it returns the substring starting at the position specified in the second argument and continuing to the end of the string. For example, substring("12345",2) returns "2345".

More precisely, each character in the string (see Strings) is considered to have a numeric position: the position of the first character is 1, the position of the second character is 2 and so on.

NOTE: 

This differs from Java and ECMAScript, in which the String.substring method treats the position of the first character as 0.

The returned substring contains those characters for which the position of the character is greater than or equal to the rounded value of the second argument and, if the third argument is specified, less than the sum of the rounded value of the second argument and the rounded value of the third argument; the comparisons and addition used for the above follow the standard IEEE 754 rules; rounding is done as if by a call to the round function. The following examples illustrate various unusual cases:

  • substring("12345", 1.5, 2.6) returns "234"

  • substring("12345", 0, 3) returns "12"

  • substring("12345", 0 div 0, 3) returns ""

  • substring("12345", 1, 0 div 0) returns ""

  • substring("12345", -42, 1 div 0) returns "12345"

  • substring("12345", -1 div 0, 1 div 0) returns ""

number string-length( string? )

The string-length returns the number of characters in the string (see Strings). If the argument is omitted, it defaults to the context node converted to a string, in other words the [string-value] of the context node.

string normalize-space(string?)

The normalize-space function returns the argument string with whitespace normalized by stripping leading and trailing whitespace and replacing sequences of whitespace characters by a single space. Whitespace characters are the same as those allowed by the S production in XML. If the argument is omitted, it defaults to the context node converted to a string, in other words the [string-value] of the context node.

string translate(string, string, string)

The translate function returns the first argument string with occurrences of characters in the second argument string replaced by the character at the corresponding position in the third argument string. For example, translate("bar","abc","ABC") returns the string BAr. If there is a character in the second argument string with no character at a corresponding position in the third argument string (because the second argument string is longer than the third argument string), then occurrences of that character in the first argument string are removed. For example, translate("--aaa--","abc-","ABC") returns "AAA". If a character occurs more than once in the second argument string, then the first occurrence determines the replacement character. If the third argument string is longer than the second argument string, then excess characters are ignored.

NOTE: 

The translate function is not a sufficient solution for case conversion in all languages. A future version of XPath may provide additional functions for case conversion.