XSLT supports all functions defined in XPath. In addition, it defines 10 extra functions. Most XSLT processors also make several extension functions available and allow you to write your own extension functions in Java or other languages. The extension API is nonstandard and processor dependent.
XPath and XSLT functions are weakly typed. Although one type or another is occasionally preferred, the processor normally converts any type you pass in to the type the function expects. Functions that only take node-sets as arguments are an exception to the weak-typing rule. Other data types including strings, numbers, and booleans cannot be converted to node-sets automatically.
XPath and XSLT functions also use optional arguments, which are filled in with defaults if omitted. In the following sections, we list the most common and useful variations of each function.
current( ) |
node-set current( ) |
document( ) |
node-set document(string uri) node-set document(node-set uris) node-set document(string uri, node-set base) node-set document(node-set uris, node-set base) |
If the URI contains a fragment identifier, then the node-set returned may indicate something other than the root node and thus contain more than one node. If an error occurs while retrieving a document, most XSLT processors stop processing the stylesheet.
The document( ) function may also take a node-set as an optional second argument, in which case the first node (in document order) in this set is used as the base URI with which to resolve relative URIs given in the first argument. If the second argument is omitted, then base URIs are resolved relative to the stylesheet's location.
element-available( ) |
boolean element-available(string qualifiedElementName) |
format-number( ) |
string format-number(number x, string pattern) string format-number(number x, string pattern, string decimalFormat) |
This function's behavior is modeled after the java.text.DecimalFormat class in Java 1.1 (not 1.2 or later). See http://java.sun.com/products/jdk/1.1/docs/api/java.text.DecimalFormat.html for full documentation of the pattern passed as the second argument.
The pattern specifies whether leading and trailing zeros should be printed, whether the number's fractional part is printed, the number of digits in a group, and the leading and trailing suffixes for negative and positive numbers. The patterns are described using an almost Backus-Naur Form grammar, given here:
pattern -> subpattern{;subpattern} subpattern -> {prefix}integer{.fraction}{suffix} prefix -> '\\u0000'..'\\uFFFD' - specialCharacters suffix -> '\\u0000'..'\\uFFFD' - specialCharacters integer -> '#'* '0'* '0' fraction -> '0'* '#'*
The first line is not pure BNF. The first subpattern is used for positive numbers. The second subpattern, which may not be present, is used for negative numbers. If it's not present, negative numbers use the positive format, but are prefixed with a minus sign. Table 23-1 defines the symbols used in the grammar.
Symbol |
Meaning |
---|---|
0 |
A digit, including leading or trailing zeros; may be set to a different character using the zero-digit attribute of xsl:decimal-format. |
# |
A digit, except for leading or trailing zero; may be set to a different character using the digit attribute of xsl:decimal-format. |
. |
A decimal separator; may be set to a different character using the decimal-separator attribute of xsl:decimal-format. |
, |
A grouping separator; may be set to a different character using xsl:decimal-format's grouping-separator attribute. |
; |
Separates the positive and negative format patterns in a format string; may be set to a different character using the pattern-separator attribute of xsl:decimal-format. |
- |
A default negative prefix; may be set to a different character using xsl:decimal-format's minus-sign attribute. |
% |
Multiplies by 100 and shows as percentage; may be set to a different character using xsl:decimal-format's percent attribute. |
Multiplies by 1,000 and shows as per mille; may be set to a different character using xsl:decimal-format's permille attribute. |
|
X |
Indicates that any other character can be used in the prefix or suffix. |
' |
Used to quote special characters in a prefix or suffix. |
The integer part contains as many digits as necessary.
The grouping separator separates every three digits.
If the integer part only contains zeros, a single zero is placed before the decimal separator.
Up to three digits are printed after the decimal point. However, any trailing zeros are not printed.
No separate pattern is included for negative numbers. Thus, negative numbers are printed the same as positive numbers, but prefixed with a minus sign.
function-available( ) |
boolean function-available(string qualifiedName) |
generate-id( ) |
string generate-id(node-set node) string generate-id( ) |
key( ) |
node-set key(string keyName, string value) node-set key(string keyName, node-set values) |
system-property( ) |
object system-property(string qualifiedPropertyName) |
XSLT processors are required to recognize and return values for these three properties:
Implementations may also recognize and return values for other processor-dependent properties.
unparsed-entity-uri( ) |
string unparsed-entity-uri(string entityName) |
Copyright © 2002 O'Reilly & Associates. All rights reserved.