Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between normalize-space(.) and normalize-space(text())?

Tags:

xpath

I was writing an XPath expression, and I had a strange error which I fixed, but what is the difference between the following two XPath expressions?

"//td[starts-with(normalize-space()),'Posted Date:')]" 

and

"//td[starts-with(normalize-space(text()),'Posted Date:')]"   

Mainly, what will the first XPath expression catch? Because I was getting a lot of strange results. So what does the text() make in the matching? Also, is there is a difference if I said normalize-space() & normalize-space(.)?

like image 324
Karim Avatar asked May 13 '11 12:05

Karim


People also ask

What is normalize space?

Use this function to trim the leading and trailing white spaces (blank spaces, tabs and new line characters), and converts multiple white spaces to a single blank space. Note: This function is only supported by the CDC Replication Engine for Event Server. Syntax: normalize-space( value )

What is the use of normalized space in XPath?

The normalize-space function strips leading and trailing white-space from a string, replaces sequences of whitespace characters by a single space, and returns the resulting string.

What is normalize space in selenium?

Normalize Space XPath Selenium normalize-space is a function in XPath which ignores all redundant spaces in a target string HTML element and and returns the resulting string. Here redundant space means: Leading Space. Trailing Space. Repeated White Space (replaces sequences of whitespace characters by a single space)

How do I normalize space with text in XPath?

If an element has spaces in its text or in the value of any attribute, then to create an xpath for such an element we have to use the normalize-space function. It removes all the trailing and leading spaces from the string. It also removes every new tab or lines existing within the string.


1 Answers

Well, the real question is: what's the difference between . and text()?

. is the current node. And if you use it where a string is expected (i.e. as the parameter of normalize-space()), the engine automatically converts the node to the string value of the node, which for an element is all the text nodes within the element concatenated. (Because I'm guessing the question is really about elements.)

text() on the other hand only selects text nodes that are the direct children of the current node.

So for example given the XML:

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

and assuming <a> is your current node, normalize-space(.) will return Foo Bar lish, but normalize-space(text()) will fail, because text() returns a nodeset of two text nodes (Foo and lish), which normalize-space() doesn't accept.

To cut a long story short, if you want to normalize all the text within an element, use .. If you want to select a specific text node, use text(), but always remember that despite its name, text() returns a nodeset, which is only converted to a string automatically if it has a single element.

like image 111
biziclop Avatar answered Sep 20 '22 18:09

biziclop