Friday

Trim / Remove spaces in Xpath?



If you are wondering how you can trim the spaces in your xpath, this is how:

The normalize-space function strips leading and trailing white-space from a string, replaces sequences of white-space characters by a single space, and returns the resulting string.

Example : Which one is better?
//td[starts-with(normalize-space(),'Text to Trim')]
//td[starts-with(normalize-space(text()),'Text to Trim')]
The second option is better as it targets at the specific nodes


More string functions in XPATH:

FunctionsDescription
starts-with(string1string2)Returns true if the first string starts with the second string.
contains(string1string2)Returns true if the first string contains the second string.
substring(stringoffsetlength)Returns a section of the string. The section starts at offset
(which is a number), and is as long as the value provided at length.
substring-before(string1string2)Returns the part of string1 up until the first occurence of string2.
substring-after(string1string2)Returns the part of string1 after the first occurence of string2.
string-length(string)Returns the length of string (i.e. the number of characters).
normalize-space(string)Trims the leading and trailing space from string.
Also replaces consecutive occurrences of white space with a single space.
translate(string1string2string3)Returns string1 after any matching characters in string2 have been
replaced by the characters in string3.
concat(string1string2...)Concatenates all strings (i.e. joins them together).
format-number(number1string1,string2)Returns a formatted string version of number1 after applying string1 as
a format string. string2 is an optional locale string.


No comments:

Post a Comment