I have been asked many times why the DBA specification requires that the text () function be added to the XPath of the value method when parsing xml values, it seems that the result of adding and not adding is the same.
The following test script shows the difference between text () and why it is required.
DECLARE
@ Xml
SET @ xml = N'
<A/>
<B> B
<C> c </c>
</B> ';
SELECT @ xml. value ('(/a) [1]', 'varchar (10 )');
SELECT @ xml. value ('(/a/text () [1]', 'varchar (10 )');
SELECT @ xml. value ('(/B) [1]', 'varchar (10 )');
SELECT @ xml. value ('(/B/text () [1]', 'varchar (10 )');
Execute this script. The obvious difference is displayed in the result.
By comparing the execution plans, we can see that there are significant differences between the two implementation plans and significant differences in efficiency. If text () is not used, additional steps are required to retrieve the data of the child node (even if the child node exists, the overhead still exists ).
Therefore, if it is not for a special purpose, you should specify the text () function in XPath for precise positioning to reduce performance overhead.