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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…