📊 XSLT/XPath Functions – Power Your XML Transformations with Built-in Logic
🧲 Introduction – Why Learn XSLT/XPath Functions?
XSLT and XPath functions provide powerful capabilities for querying, filtering, and transforming XML data. These built-in functions allow you to perform logic, manipulate strings, numbers, and node sets, and dynamically generate content during XSLT transformations. Understanding these functions is essential for building effective and expressive stylesheets.
🎯 In this guide, you’ll learn:
- What XSLT and XPath functions are and how they’re used
- Common categories: node, string, number, boolean, and date functions
- Real-world examples using functions inside
<xsl:if>
,<xsl:value-of>
, and more - Best practices for clean and efficient transformations
📘 What Are XSLT/XPath Functions?
XSLT uses the XPath language to locate and manipulate nodes. XPath includes functions that operate on:
- Node sets
- Strings
- Numbers
- Booleans
- Dates (XPath 2.0+ or with extension functions)
✅ These functions are typically used in select
, test
, or match
attributes.
🔑 Core Categories of XPath/XSLT Functions
🔍 Node Set Functions
Function | Description |
---|---|
last() | Returns the position of the last node |
position() | Returns the position of the current node |
count(node-set) | Returns number of nodes in a node-set |
name() | Returns the name of a node |
local-name() | Returns the local part of a qualified name |
🔤 String Functions
Function | Description |
---|---|
string() | Converts a value to a string |
concat(str1, str2, ...) | Joins multiple strings |
substring(str, start, len) | Extracts part of a string |
contains(str, substr) | Checks if one string contains another |
starts-with(str, prefix) | Checks if a string starts with a prefix |
string-length(str) | Returns the length of a string |
normalize-space(str) | Removes extra whitespace |
translate(str, from, to) | Character-by-character replacement (like replace) |
🔢 Number Functions
Function | Description |
---|---|
number(value) | Converts to a number |
floor(num) | Rounds down |
ceiling(num) | Rounds up |
round(num) | Rounds to nearest integer |
sum(node-set) | Adds numeric values in a node set |
✅ Boolean Functions
Function | Description |
---|---|
boolean(value) | Converts to boolean |
not(expr) | Logical NOT |
true() | Returns true |
false() | Returns false |
lang(string) | Checks if current node is in specified language |
🧪 Example – Using XPath Functions in XSLT
XML:
<books>
<book><title>XML A</title><price>30</price></book>
<book><title>XML B</title><price>50</price></book>
</books>
XSLT:
<xsl:template match="/books">
<xsl:for-each select="book">
<p>
Title: <xsl:value-of select="title"/>
- Price: <xsl:value-of select="price"/>
<xsl:if test="price > 40">
(Expensive)
</xsl:if>
</p>
</xsl:for-each>
</xsl:template>
✅ Uses comparison function (price > 40
) and node access.
🎛️ Practical Function Combinations
<xsl:value-of select="concat(upper-case(title), ' costs $', price)"/>
<xsl:if test="contains(author, 'John') and string-length(author) > 5">
<strong>Popular Author</strong>
</xsl:if>
🔍 XPath 2.0+ Functions (if supported)
Function | Description |
---|---|
upper-case(str) | Converts to uppercase |
lower-case(str) | Converts to lowercase |
matches(str, regex) | Tests string against regex |
replace(str, pattern, replacement) | Regex-based replacement |
📝 These are only available in XPath 2.0/XSLT 2.0+ (not supported by all engines).
✅ Best Practices for XSLT Functions
- ✔️ Use
normalize-space()
to clean up inconsistent XML input - ✔️ Use
concat()
instead of string concatenation (+
) - ✔️ Keep expressions readable—split into variables if needed
- ✔️ Avoid deeply nested functions—use
xsl:variable
to simplify - ❌ Don’t assume XPath 2.0 functions work—check your processor version
- ❌ Avoid arithmetic on non-numeric content without
number()
conversion
📌 Summary – Recap & Next Steps
XPath and XSLT functions enable dynamic, conditional, and formatted transformations of XML data. From extracting and cleaning strings to filtering nodes, they are essential tools for writing expressive stylesheets.
🔍 Key Takeaways:
- Functions are used in
select
,test
, and expressions - Categories include string, node, number, and boolean functions
- XPath 2.0 adds regex, case-changing, and type-checking functions
⚙️ Real-world relevance: Used in XML reports, data cleaning, financial calculations, document publishing, and metadata transformation.
❓ FAQs – XSLT/XPath Functions
❓ Can I create custom functions in XSLT?
✅ Only with XSLT extension libraries or XSLT 2.0+. Not in pure XSLT 1.0.
❓ Are XPath 2.0 functions supported everywhere?
❌ No. Many browsers and processors still use XPath 1.0.
❓ What’s the difference between string()
and text()
?
✅ string()
converts a node or value to a string. text()
is a node selector for text nodes.
❓ Can I replace characters in a string?
✅ Use translate()
in XPath 1.0 or replace()
in XPath 2.0.
❓ How can I calculate a total from XML data?
✅ Use sum(//price)
to add all <price>
values.
Share Now :