Can someone explain the difference between text() and string() functions. I often use one with other, but it does not make any difference, both will get the string value of the xml node.
Can someone explain the difference between text() and string() functions. I often use one
Share
I.
text()isn’t a function but a node test.It is used to select all text-node children of the context node.
So, if the context node is an element named
x, thentext()selects all text-node children ofx.Other examples:
selects all text-node children of any
celement that is a child of anybelement that is a child of the top elementa.II. The
string()functionBy definition
string(exprSelectingASingleNode)returns the string value of the node.The string value of an element is the concatenation of all of its text-node descendents — in document order.
Therefore, if in the following XML document:
string(/a)returns (without the surrounding quotes):As we see, the string value reflects three white-space-only text-nodes, which we typically fail to notice and account for.
Some XML parsers have the option of stripping-off white-space-only text nodes. If the above document was parsed with the white-space-only text nodes stripped off, then the same function:
now returns: