Fuzzy search for XML using Xpath

Source: Internet
Author: User

Fuzzy search for XML files is troublesome. The Xpath expressions are not like "*" or "? "Or fuzzy search wildcards like" % "in SQL expressions. But fortunately, the Xpath function provides functions such as contains and match.

ContainsIs a string lookup Function
Syntax: fn: contains (string1, string2), which indicates that if string1 contains string2, true is returned; otherwise, false is returned.
For example, contains ('xml', 'xm '). The result is true.

MatchIs a function that matches regular expressions.
Syntax: fn: matches (string, pattern), which indicates that if the string parameter matches the specified mode, true is returned; otherwise, false is returned.
For example, matches ("12", "[0-9] {1, 2}"), and the result is true.

For some basic Xpath knowledge, refer to the BizTalk Development Series (34) Xpath, so that you can probably know how to perform fuzzy search for XML. Below we will perform several tests based on the previous instance.

 

XML used for testing

<Root>
<Person ID = "1001">
<Name lang = "zh-cn"> Zhang chengbin </Name>
<Email xmlns = "www.quicklearn.cn"> cbcye@live.com </Email>
<Blog> http://cbcye.cnblogs.com </Blog>
</Person>
<Person ID = "1002">
<Name lang = "en"> Gary Zhang </Name>
<Email xmlns = "www.quicklearn.cn"> GaryZhang@cbcye.com </Email>
<Blog> http://www.quicklearn.cn </Blog>
</Person>
</Root>

 

Tools used:XMLSpyNote that an open-source Xpath expression editing tool is mentioned earlier: the SketchPath cannot correctly display the query results when executing the query statement. Therefore, XMLSpy is recommended for the following tests.

1. query the Person nodes with cn strings in all Blog node values

Xpath expression:/Root // Person [contains (Blog, 'cn')]

Result:

2. query the Person nodes with cn strings in all Blog node values and 01 In the attribute ID value

Xpath expression:/Root // Person [contains (Blog, 'cn') and contains (@ ID, '01')]

3. query the values of the Email nodes restricted by the namespace with the "live" string and the Blog node value with the cn string.

Xpath expression:/Root/Person // * [local-name () = 'e-mail 'and contains (text (), 'live')]/parent: Person

Result:

4. Hybrid query of nodes restricted by namespaces and nodes and attributes not restricted by namespaces

Xpath expression:/Root/Person // * [local-name () = 'e-mail 'and contains (lower-case (text (), 'live')] [contains (.. /Blog, 'cn')] [contains (.. /Name/@ lang, 'zh-cn')]/parent: Person

Result:

5. query the nodes with Email constructor values for all nodes.

Xpath expression: // * [matches (text (), '\ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) * ')]

Result:

 

Through the above tests, fuzzy queries by using the contrains and match functions can basically meet the basic usage requirements. In addition, this article only lists several basic examples. In actual use, you need to flexibly use functions and axes to construct Xpath expressions to meet your needs.

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.