Difference between normalize space (.) and normalize space (text ()) in XPath

Normalize, literally means normalization, plus space, roughly means the processing of space.

The official explanation is as follows:

By removing leading and trailing whitespace and replacing a series of whitespace characters with a single space, the whitespace is standardized. If this parameter is omitted, the string value of the context node is normalized and returned.

. is the current node. If you use it where you need a string (for example, as a parameter normalize space() ), the engine automatically converts the node to the string value of the node, which, for the element, is all the text nodes connected within the element.

text () on the other hand, only the text node that is the direct child of the current node is selected.

For example, given XML:

<a&>Foo
    <b&>Bar</b&> lish </a&> 

Assuming & lt; a &> is your current node, normalize space (.) will return foo bar list , but normalize space (text ()) will fail because text () returns a node set of two text nodes ( foo and List ), which normalize space () does not accept.

To make a long story short, if you want to standardize all the text in an element, use . . If you want to select a specific text node, use text() , but always remember that although the name is different, text() returns a node set, which is automatically converted to a string if it has only one element.

Similar Posts: