XSLT, XPath, and XQuery provide robust support for manipulating and querying XML data. They come with a wide range of built-in functions to handle string operations, numeric calculations, node manipulations, and more. This guide by The Coding College provides a detailed overview of the functions available in these technologies.
Core Function Categories
- String Functions
- Numeric Functions
- Date and Time Functions
- Node Functions
- Sequence Functions
- Boolean Functions
These functions are standardized under the XPath and XQuery Functions and Operators 3.1 specification.
1. String Functions
string()
Converts an argument to a string.
Syntax:
string(expression)
Example:
string(123) <!-- Result: "123" -->
concat()
Concatenates two or more strings.
Syntax:
concat(string1, string2, ...)
Example:
concat("Hello", " ", "World!") <!-- Result: "Hello World!" -->
contains()
Checks if a string contains a specific substring.
Syntax:
contains(string, substring)
Example:
contains("codingcollege", "coding") <!-- Result: true -->
substring()
Extracts a part of a string.
Syntax:
substring(string, start, length)
Example:
substring("TheCodingCollege", 4, 6) <!-- Result: "Coding" -->
normalize-space()
Removes leading and trailing spaces and replaces sequences of whitespace with a single space.
Syntax:
normalize-space(string)
Example:
normalize-space(" Hello World ") <!-- Result: "Hello World" -->
2. Numeric Functions
sum()
Calculates the sum of a sequence of numbers.
Syntax:
sum(sequence)
Example:
sum((1, 2, 3, 4)) <!-- Result: 10 -->
round()
Rounds a number to the nearest integer.
Syntax:
round(number)
Example:
round(3.14) <!-- Result: 3 -->
floor()
Returns the largest integer less than or equal to the given number.
Syntax:
floor(number)
Example:
floor(3.9) <!-- Result: 3 -->
ceiling()
Returns the smallest integer greater than or equal to the given number.
Syntax:
ceiling(number)
Example:
ceiling(3.1) <!-- Result: 4 -->
3. Date and Time Functions
current-date()
Returns the current date.
Syntax:
current-date()
Example:
current-date() <!-- Result: "2024-12-27" -->
current-time()
Returns the current time.
Syntax:
current-time()
Example:
current-time() <!-- Result: "14:35:00" -->
current-dateTime()
Returns the current date and time.
Syntax:
current-dateTime()
Example:
current-dateTime() <!-- Result: "2024-12-27T14:35:00" -->
4. Node Functions
name()
Returns the name of a node.
Syntax:
name(node)
Example:
name(/book/title) <!-- Result: "title" -->
count()
Counts the number of nodes in a sequence.
Syntax:
count(node-set)
Example:
count(/books/book) <!-- Result: Number of <book> elements -->
position()
Returns the position of a node in a sequence.
Syntax:
position()
Example:
/xsl:for-each[position() = 1] <!-- Result: First node -->
5. Sequence Functions
distinct-values()
Returns distinct values from a sequence.
Syntax:
distinct-values(sequence)
Example:
distinct-values((1, 2, 2, 3)) <!-- Result: (1, 2, 3) -->
index-of()
Returns the index positions of a value in a sequence.
Syntax:
index-of(sequence, value)
Example:
index-of((1, 2, 3, 2), 2) <!-- Result: (2, 4) -->
6. Boolean Functions
not()
Returns the negation of a boolean value.
Syntax:
not(expression)
Example:
not(true()) <!-- Result: false -->
true()
Returns the boolean value true
.
Syntax:
true()
false()
Returns the boolean value false
.
Syntax:
false()
Using Functions in XSLT and XQuery
- In XSLT, functions are used in expressions and templates to manipulate data.
- In XPath, functions query XML documents and retrieve or transform data.
- In XQuery, functions query XML data and generate new XML documents.
Example in XSLT:
<xsl:value-of select="concat('Hello, ', /user/name)"/>
Example in XQuery:
let $name := "John"
return concat("Hello, ", $name)
Conclusion
Functions are an essential part of working with XML transformations and queries. Whether you’re using XSLT, XPath, or XQuery, mastering these functions will help you write more powerful and flexible scripts.
For more tutorials, examples, and study resources on XML, XSLT, XPath, and XQuery, visit The Coding College today!