XPath function
translate()
Character-by-character replacement, and the only way to fold case in XPath 1.0 — because lower-case() is 2.0.
translate(s, from, to) replaces each character of s that appears in `from` with the character at the same position in `to`. Characters in `from` with no counterpart in `to` are deleted, which makes it a way to strip characters as well as substitute them.
Its most common use is case-insensitive matching, since XPath 1.0 has neither lower-case() nor upper-case(). The idiom is verbose and unavoidable.
- Syntax
translate(string, from, to) → string- Returns
- string
- Kind
- Function
- Version
- XPath 1.0 (libxml2)
Worked examples
Each result below is what the evaluator returned, asserted on every test run. An empty result means an empty node-set — which is an answer, not an error.
translate('WIDGET-1', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')on order.xmlRunwidget-1translate('2026-01-01', '-', '')on order.xmlRun20260101An empty `to` deletes the characters instead of replacing them.
count(//line[contains(translate(@sku, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'widget')])on order.xmlRun2Case-insensitive contains(), the long way round.
What catches people out
- It is per character, not per substring — translate('abc', 'ab', 'x') does not replace 'ab' with 'x'.
- A shorter `to` deletes the surplus characters rather than leaving them alone. That is a feature, and a trap.
- It only handles the characters you list. Accented letters need to be in the mapping explicitly.
The sample documents
Every example on this page runs against this document. The sitemap one is namespaced on purpose — almost every real XML document is, and that changes which expressions match.
<?xml version="1.0" encoding="UTF-8"?>
<order id="A-1001" tier="gold">
<customer country="NO">Ada Lovelace</customer>
<lines>
<line sku="WIDGET-1" qty="2"><price>14.50</price></line>
<line sku="WIDGET-2" qty="1"><price>39.00</price></line>
<line sku="GIFT-WRAP" qty="1"><price>0.00</price></line>
</lines>
<note> Deliver after 09:00 </note>
</order>Related
Get started
Bring order to the XML your team can't afford to ignore.
Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.