Skip to main content
XMLDir

Search 169 pages — formats, namespaces, parser messages, schema errors and tools.

XPath function

substring-before()

Everything before the first occurrence of a marker — and the empty string when the marker is absent, which is not an error you will notice.

substring-before(s, m) returns the part of s that precedes the first m. It is the standard way to split a delimited value without regular expressions.

When m does not occur, the result is the empty string. That failure is silent, so a mistyped delimiter produces empty output rather than a complaint.

Syntax
substring-before(string, marker) → 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.

  • substring-before('WIDGET-1', '-')on order.xmlRun
    WIDGET
  • substring-before('2026-01-01', '-')on order.xmlRun
    2026
  • substring-before('WIDGET', '-')on order.xmlRun

    (empty node-set — no matches)

    No marker, so the empty string — printed as nothing at all.

Run your own expression

What catches people out

  • A missing marker gives the empty string, not the whole input. That is the opposite of what several other languages do.
  • Only the first occurrence counts. To split on the last, you need nested substring-after calls or recursion.

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.

order.xml
<?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

All 34 XPath entries

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.