Some tips for using XPath syntax

Source: Internet
Author: User

XPath syntax is still very powerful, support some functions and operators, convenient operation, but because of the version of the possible functions can only be used in XPath2 (2007), and not in XPath1 (1999), such as function Ends-with ( String).

XPath syntax in the selection of DOM object search is very useful, there are many on the internet on the use of XPath syntax, but all roughly the same, in fact, the main search can be divided into three aspects, (1) The search for the node, (2) The node element search, (3) Search for node properties in order to illustrate the convenience, first show a DOM tree tree for this diagram, we use the XPath syntax to say the search traversal of the wording, if there are errors, please the WI friends to criticize.

<?xml version= "1.0"?>

<navi>

<navi prikey= "SQ" >

      <nametext= "A-level" value= "Yhl_aj"/>

<nametext= "Class B" value= "YHL_BJ"/>

</navi>

<navi prikey= "SB" >

<nametext= "Class B" value= "BZL_BJ"/>

&NBSP;&NBSP; </navi>

</navi>

&NBSP; 1) Searching for a node requires only one principle to search for a node, Position the search path to the path of the node you are looking for, as in the form: ". Father/father ' s father/father/i want ". For example, I want to find the name node, I would like to write the XPath: "/navi/navi/name", you want to find the name node, so you can locate the/name on the line, but you look at the picture, you will find three eligible nodes, maybe you just want to find <nametext = "B-level" value= "BZL_BJ"/> This node, then add some conditions such as you can write XPath: "/navi/navi[@prikey =\ ' sb\ ']/name", which is written by restricting the conditions of the parent path, there is another way: "/ navi/navi/name[@value =\ ' bzl_bj\ '), which is limited by the nature of name itself. But no matter how it is written, want to find that node, first locate it on the path of XML, and then use the two methods just mentioned to do a unique brush selection. Regardless of the node at that level is similar.

&NBSP; 2) Search for node elements searches for node elements only follows a principle, Navigate to the node where the element is located, and then add the//element name. The notation is: ". Father/this node//i want ". If you think of name as a NAVI element, then the search for these name elements can be so positioned"/navi/navi//name ", the first part of the node's positioning without repeating, If you want to further locate which name you want, simply write "/navi/navi//name[@value =\ ' bzl_bj\ ']" so easy!!! If you want to look from this DOM, then the first half of the node path is not required, so write directly: "//name[@value = ' * * * *]"

3" the search for attributes is also very simple, the wording of the form: ". Father/thiselement//@i want ". For example, if you want to search the Text property of <name text= "B-Level" value= "BZL_BJ"/>, simply write "/navi/navi[@prikey =\ ' sb\ ']/name//@text". If you want to search through this DOM object and write "//@text", then you get an array of text collections. These are all as arguments to selectnodes (* *) or selectSingleNode (* *) to get what you want.

in a word: "A bar node two bar elements, plus a @ attribute with me."



From for notes (Wiz)

Some tips for using XPath syntax

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.