What is the purpose of normalize-space()?

人盡茶涼 提交于 2021-01-27 14:20:48

问题


I don't understand very well the purpose of normalize-space(). I think it's very useful. In fact, I always use it when I am doing tests, but I am not sure the principal purpose.

For example, in these two cases, what are the differences? What is the advantage of using it?

Example 1

WebElement seleccionLabelCabecera 
  = findBy(xpath("(//div[contains(normalize-space(@class), 
                                  'windowViewMode-maximized active')"]

Example 2

WebElement seleccionLabelCabecera 
  = findBy(xpath("(//div[@class, 'windowViewMode-maximized active')"]

回答1:


The normalize-space() function simplifies specification of tests against strings for which whitespace variations are insignificant.

In your examples, consider that additional whitespace before, between, or after the two class values ought not have bearing on whether your targeted div is found. By removing leading and trailing whitespace and consolidating internal whitespace to a single space, normalize-space() simplifies tests where whitespace variations do not matter.




回答2:


normalize-space xpath function will remove leading and trailing white space.

[source msdn] White space is normalized by stripping leading and trailing white space and replacing sequences of white space characters with a single space. If the argument is omitted, the string-value of the context node is normalized and returned.

Example:

<text>
    This is a
  test, with a   lot    of 
    irregular  spacing and
    waiting to be   normalizaed.


</text>

Result with out normalize-space

"

This is a

test, with a lot of

irregular spacing and

waiting to be normalizaed.

"

Result with normalize-space

"This is a test, with a lot of irregular spacing and waiting to be normalized."

You can get more detailed explanation in the below link

https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256063(v=vs.100)



来源:https://stackoverflow.com/questions/55854177/what-is-the-purpose-of-normalize-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!