What is the difference between normalize-space(.) and normalize-space(text())?
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()
& nor开发者_如何学编程malize-space(.)
?
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.
精彩评论