1

There are two XPath expressions:

//LI/A[normalize-space()="fullscreen"]
//LI/A["fullscreen"]

As I understand, normalize-space() is used to trim strings, as said in documentation. So the two expressions should have the same meaning?

But the first one can be evaluated to locate the element in this web page by:

document.evaluate('//LI/A[normalize-space()="fullscreen"]', document).iterateNext();

, while the second will locate a different element.

What's the difference between them?

zhm
  • 3,513
  • 3
  • 34
  • 55

1 Answers1

1

The second expression does not actually make much sense. The only condition you set to filter a elements is the "fullscreen" string, which will always evaluate to true. In other words, //LI/A["fullscreen"] is equivalent to //LI/A.

You probably meant to check the element's string value against fullscreen:

//LI/A[. = "fullscreen"]

It is also important to note that when normalize-space() is called with no arguments, it will automatically use the string value of the current node as an argument. The node's string value for an element is all text nodes within the element concatenated.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • As I understand, `normalize-space()="fullscreen"` means call `normalize-space()` on `"fullscreen"`, and put the return value into `[]`. Correct? If not, what does it mean? – zhm Apr 21 '17 at 07:26
  • @MartinZhai I've added some more information into the answer, hope it makes things a little bit clearer. Thanks. – alecxe Apr 21 '17 at 07:32