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(.)?
Well, the real question is: what’s the difference between
.andtext()?.is the current node. And if you use it where a string is expected (i.e. as the parameter ofnormalize-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:
and assuming
<a>is your current node,normalize-space(.)will returnFoo Bar lish, butnormalize-space(text())will fail, becausetext()returns a nodeset of two text nodes (Fooandlish), whichnormalize-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, usetext(), 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.