This has nothing to do with normalize-space(), and everything to do with text()
.
text()
is short for child::text()
, and selects the text nodes that are immediate children of the label element. Unless you are stripping whitespace text nodes, the label element in your example has two child text nodes, one of which is all whitespace, the other contains "some label" surrounded by whitespace.
BTW, which syntax is better: //label[text()[normalize-space() = 'some label']] vs //label[normalize-space(text()) = 'some label'] and why?
They do different things; the one that is better is the one that does what you want to achieve.
In XPath 1.0, the first expression selects label elements that have a child text node whose value, after whitespace normalization, equals "some label". The second selects label elements whose first child text node, after whitespace normalization, equals "some label". That's because normalize-space() (like all functions that expect a string), if you give it a node-set, takes the string value of the first node in the node-set.
In XPath 2.0, the first expression selects label elements that have a child text node whose value, after whitespace normalization, equals "some label". The second selects label elements if they have a child text node, after whitespace normalization, equals "some label", but raises an error if the label element has more than one child text node. That's because normalize-space() (like all functions that expect a string), atomizes its argument, and reports a type error if the length of the atomized sequence is greater than one.