In XML. value, the importance of using the text () function in xpath

Source: Internet
Author: User

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 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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.