Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
Usage
One situation where this function is useful is when calculating the size of a table. If you have a sequence
$ns
and you want to arrange the values in three columns, then the number of rows needed is:
ceiling(count($ns) div 3)
.
Although the result is numerically equal to an integer, it does not necessarily have the type
xs:integer
. You can force it to an integer by using the
xs:integer()
constructor function, for example,
xs:integer(ceiling(count($ns) div 3))
.
See Also
floor()
on page 779
round()
on page 870
idiv
operator on page 574 in Chapter 8
codepoint-equal
The
codepoint-equal()
function compares two strings character by character: the strings are equal if they contain the same sequence of characters, regardless of the default collation in force.
Signature
Argument | Type | Meaning |
value-1 | xs:string? | The first string to be compared |
value-2 | xs:string? | The second string to be compared |
Result | xs:boolean? | true if the two strings contain the same characters |
Effect
The function compares the two strings character by character, and returns true only if the strings are identical in terms of Unicode codepoints.
If either
value-1
or
value-2
is an empty sequence, the result is an empty sequence. This is an exception to the usual rule that string-handling functions treat an empty sequence as a zero-length string.
If the default collation is the Unicode codepoint collation, the result is exactly the same as comparing the strings using the XPath
eq
operator. The difference is that this function does not depend on how the default collation has been set up in the context. It is therefore useful when comparing strings such as filenames or purchase order numbers, where use of natural-language collations is inappropriate.
Although the function signature requires the arguments to be strings, the function calling rules ensure that it will also accept
xs:anyURI
values. In fact, it is particularly appropriate for comparing URIs and was added to the function library for that reason.
Examples
Expression | Result |
codepoint-equal(“http://www.w3.org/”, “http://www.w3.org/”) | true |
codepoint-equal(“http://www.w3.org/”, “HTTP://www.w3.org/”) | false |
codepoint-equal(“”, “”) | true |
codepoint-equal((), “banana”) | () |
See Also
compare()
on page 727
eq
operator in Chapter 8 on page 582
codepoints-to-string
The
codepoints-to-string()
function takes as input a sequence of integers representing the Unicode codepoint values of the characters in a string, and returns the corresponding string. For example,
codepoints-to-string((65,66,67))
returns the string
“ABC”
.
Signature
Argument | Type | Meaning |
codepoints | xs:integer* | The sequence of codepoints. These must represent characters that are valid in XML 1.0 or XML 1.1, depending on the version that the processor supports. |
Result | xs:string | The string consisting of characters with the given codepoint values . |
Effect
The function returns a string whose characters correspond to the Unicode codepoints in the supplied sequence.
A character whose codepoint is above xFFFF must be supplied as a single integer value, not as two code values forming a surrogate pair.
If the supplied sequence is empty, the result will be a zero-length string.
A common case, of course, is where the sequence of codepoints contains a single integer, in which case the resulting string will be of length one.
Integers that do not represent valid codepoints cause a runtime error. This includes the case of codepoints that are valid in Unicode, but not in XML (for example the integer zero).
Examples
Expression | Result |
codepoints-to-string((65, 83, 67, 73, 73)) | “ASCII” |
codepoints-to-string(48 to 57) | “0123456789” |
codepoints-to-string(()) | The zero-length string ( “” ) |
codepoints-to-string(64+$n) | The nth letter of the English alphabet |
Usage
There are two main ways of using this function: as a way of constructing a string algorithmically, and as a complement to the function
string-to-codepoints()
.
As an example of the first kind of application, suppose you need to construct the hexadecimal representation of an integer. This might make use of an expression to return a single hex digit representing a value in the range 0–15. Here is a possible way of writing this expression:
codepoints-to-string(if ($d<10) then (48+$d) else (87+$d))
Personally, I prefer to code this as:
substring(“0123456789abcdef”, $d+1, 1)
As an example of the second kind of application, suppose that you want to reverse the order of the characters in a string. One way of doing this is:
codepoints-to-string(reverse(string-to-codepoints($s)))
In this example, the two functions
string-to-codepoints()
and
codepoints-to-string()
are being used simply as a way of breaking the string into a sequence of characters, and reassembling the characters into a string; the fact that the characters are represented by Unicode codepoints has no relevance.