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 Makes Programs
  • 1,493
  • 2
  • 17
  • 39

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,681
  • 1
  • 24
  • 39
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
  • 28,498
  • 28
  • 50
  • 59
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
  • 106,133
  • 27
  • 181
  • 240