1

I have the following HTML:

<p>
    Some cool text
    <a href="#">looks like</a>
    this.
</p>

I want to grab the text as a single line:

Some cool text looks like this.

I'm currently using the following XPath query:

//p//text()

And it returns all the text, but as separate lines:

Some cool text

looks like

this.

Any thoughts on how to modify my query so it can return on a single line?

The query needs to meet XPath1.0 requirements.

Community
  • 1
  • 1
Bryant James
  • 1,284
  • 2
  • 12
  • 36

3 Answers3

2

text() selects individual text nodes, and there will always be at least one text node in between elements (where there is text). What you want is to convert the p to a string:

//p/string(.)
wst
  • 11,509
  • 1
  • 22
  • 37
2

If you have XPath-2.0 or above available you can use string-join(...) to merge the text() values:

string-join(normalize-space(p))
zx485
  • 26,827
  • 28
  • 51
  • 55
2

This XPath (1.0 on up),

string(normalize-space())

will return

"Some cool text looks like this."

in a single line, as requested.

See also Testing text() nodes vs string values in XPath

kjhughes
  • 98,039
  • 18
  • 159
  • 218