Skip to main content
XMLDir

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

XPath function

starts-with()

Whether a string begins with another. XPath 1.0 has no ends-with() — that is 2.0.

starts-with(a, b) is true when a begins with b. Same argument order as contains(): the string being tested comes first.

There is no ends-with() in XPath 1.0. The idiom is a substring comparison against the tail: substring(a, string-length(a) - string-length(b) + 1) = b.

Syntax
starts-with(haystack, prefix) → boolean
Returns
boolean
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.

  • count(//line[starts-with(@sku, 'WIDGET')])on order.xmlRun
    2
  • starts-with('WIDGET-1', 'WID')on order.xmlRun
    true
  • substring('WIDGET-1', string-length('WIDGET-1') - string-length('-1') + 1) = '-1'on order.xmlRun
    true

    The ends-with() that XPath 1.0 does not have.

Run your own expression

What catches people out

  • ends-with() does not exist in XPath 1.0 and will report as an unregistered function.
  • Case-sensitive, like every string comparison in XPath 1.0.
  • starts-with(x, '') is true for every x.

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.