1

Hi friends I am not very familiar with XPATH, and have a small question:

If I have a node that looks like this:

<books>
  <book>   book name.  </book>
</books>

and I ask for this expression:

//books/book

then, is there a way for the xpath to return the normalize-string()'d version of the book name? as in, replace " book name. " and return only "book name." without any trailing or leading spaces? is that even possible with xpath.

Felipe Valdes
  • 1,998
  • 15
  • 26
  • Does this answer your question? [xpath expression to remove whitespace](https://stackoverflow.com/questions/11776910/xpath-expression-to-remove-whitespace) – Sorceri Feb 25 '22 at 18:20
  • Hi, no, unfortunately, I ended up just using Trim() in golang, which sucks because I wanted an xpath only solution... oh well... – Felipe Valdes Mar 17 '22 at 03:06

1 Answers1

0

Wouldn't this work?

normalize-space(/books/book/text())
OldProgrammer
  • 12,050
  • 4
  • 24
  • 45
  • 2
    If it's XPath 2.0+, you could just do `/books/book/normalize-space()` – Daniel Haley Feb 25 '22 at 20:59
  • While `normalize-space()` is obviously the extremely common solution to this problem, please note it does more than just trim leading and trailing space. It also reduces internal sequences of whitespace characters. – David Denenberg Feb 26 '22 at 15:19