Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath expression to remove whitespace

Tags:

xpath

I have this HTML:

 <tr class="even  expanded first>    <td class="score-time status">      <a href="/matches/2012/08/02/europe/uefa-cup/">              16 : 00       </a>     </td>           </tr> 

I want to extract the (16 : 00) string without the extra whitespace. Is this possible?

like image 941
adellam Avatar asked Aug 02 '12 11:08

adellam


People also ask

How to remove white space in XPath?

normalize-space() produces a new string from its argument, in which any leading or trailing white-space (space, tab, NL or CR characters) is deleted and any intermediary white-space is replaced by a single space character.

Which XPath function would you use to trim a text?

XPath/XQuery normalize-space function Returns the value of $arg with leading and trailing whitespace removed, and sequences of internal whitespace reduced to a single space character.

How does XPath handle spaces?

If an element has spaces in its text or in the value of any attribute, then to create an xpath for such an element we have to use the normalize-space function. It removes all the trailing and leading spaces from the string. It also removes every new tab or lines existing within the string.

Which of the following XPath function is used to trim trailing spaces?

The normalize-space(String s) method is used to normalize a string i.e. to remove any leading or trailing spaces from the string s passed as parameter to the XPath function.


1 Answers

I. Use this single XPath expression:

translate(normalize-space(/tr/td/a), ' ', '') 

Explanation:

  1. normalize-space() produces a new string from its argument, in which any leading or trailing white-space (space, tab, NL or CR characters) is deleted and any intermediary white-space is replaced by a single space character.

  2. translate() takes the result produced by normalize-space() and produces a new string in which each of the remaining intermediary spaces is replaced by the empty string.


II. Alternatively:

translate(/tr/td/a, ' &#9;&#10;&#13', '') 
like image 168
Dimitre Novatchev Avatar answered Sep 21 '22 11:09

Dimitre Novatchev