2

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

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

This would be far more overhead and far more code than the pure-XPath approach, though. Is there any specific benefit for this approach?
@MichaelWard Would it? RegEx has a reputation for being slow, but this doesn't actually need a RegEx (if it did, I'm not sure you could do it in XPath!). Meanwhile XPath is notoriously opaque. Retrieving the whole node and then doing substring searches seems likely to be easier to maintain and I doubt slower.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.