user7982813
user7982813

Reputation: 151

How do I create an XPath query to extract a substring of text?

I am trying to create xpath so that it only returns order number instead of whole line.Please see attached screenshot enter image description here

Upvotes: 2

Views: 3934

Answers (2)

Homewrecker
Homewrecker

Reputation: 1106

You could also just use your normal Xpath selector to get the complete text, being "Your oder # is: 123456" and then perform a regex on the string like mentioned in Get numbers from string with regex

Upvotes: 1

Michael Ward
Michael Ward

Reputation: 526

What you want is the substring-after() function -

fn:substring-after(string1,string2)

Returns the remainder of string1 after string2 occurs in it

Example: substring-after('12/10','/')

Result: '10'

For your situation -

substring-after(string(//p[contains(text(), "Your order # is")]), ": ")

To test this, I modified the DOM on this page to include a "Order Number: ####" string.

See it in action: Screenshot of the DOM Me using the same function to extract a similar string

Upvotes: 2

Related Questions