string()
will always return a string, whereas data()
returns the "typed value". Most of the time this will be xs:string
; however, it can also be xs:untypedAtomic
or any other atomic type (typically defined in a schema).
There are some unintuitive cases too. For example, getting data()
on a document node will return the string value of the node, but as an xs:untypedAtomic
. As a general rule, if you want a string, use string()
not data()
.
See the spec for all the details: http://www.w3.org/TR/xpath-functions/#func-data.
And this section has a very good explanation of the difference between typed- and string-data http://www.w3.org/TR/xpath-datamodel/#typed-string-relationships.