XPath on nested elements with text() but no whitespace

10,936

Use:

//div
   [.//text()
        [normalize-space() = 'i need this text']
   ]
    //text()[normalize-space()]

This selects any non-whitespace-only text node descendant of any div in the document, that (the div) has a text-node descendant whose normalized string value is the string "i need this text".

The normalize-space() function takes a string (the string value of the context node -- if no argument is specified) and produces from it another string in which all leading and trailing whitespace characters are deleted, and any inner group of adjacent whitespace characters is replaced by a single space.

Share:
10,936
Jay
Author by

Jay

Updated on July 18, 2022

Comments

  • Jay
    Jay almost 2 years

    I have poor XHTML I need to parse with xpath. It looks like this:

    <div class="foo">
      i need this text
      <br/>
      <br/>
      <span>sometext</span>
    </div>
    
    <div class="foo">
      <span>some other text</span>
      <span>sometext</span>
    </div>
    

    I want to select ALL content with "i need this text" in the first div. My problem is, that the div elements contain whitespaces or other stuff, so that //div[@class="foo"]/text() is returning empty strings for the second div also. I want to ignore these empty fields, how can I do that?

  • Marcel Burkhard
    Marcel Burkhard over 8 years
    Please add some text.
  • v.slobodzian
    v.slobodzian about 4 years
    It's not correct to use a contains function in this context. This XPath will work for "Applications 1" as well as for just "Applications". This could cause incorrect behavior and bugs.