Introduction to XML-XPATH syntax

Source: Internet
Author: User
When using dom4j, we cannot obtain an element across layers. it must be obtained at one layer, which is very troublesome.


Why xpath?

When using dom4j, we cannot obtain an element across layers. it must be obtained at one layer, which is very troublesome.
Therefore, we can use the xpath technology to access a node more conveniently, which allows us to conveniently read the specified node.

Xpath is usually used in combination with dom4j, and if you want to use xpath, you need to introduce a new package jaxen-1.1-beta-6.jar

The basic syntax of xpath includes the following:

1. The basic xpath syntax is similar to locating a file in a file system./The path indicates the absolute path to an element.

(1)/AAAIt indicates selecting the root element AAA

Here
     
     
     
     
         
      
      
  Here
 

(2)/AAA/CCCTo select all CCC sub-elements of AAA

    
     
 Here
     
     
         
      
      
  Here
 

(3)/AAA/DDD/BBBTo select all BBB sub-elements of the sub-element DDD of AAA.

    
     
     
     
     
         
  Here
      
  
 

So how can we use xpath in dom4j? It is actually very simple:

// 1. get the SAXReader parser SAXReader saxReader = new SAXReader (); // 2. specifies the file to be parsed, Document document = saxReader. read (new File (path); // 3. you can use xpath to read // document at will. selectNodes (args) returns multiple elements // document. selectSingleNode (args) returns a single element List nodes = document. selectNodes ("/AAA/BBB ");

After obtaining the document Object through dom4j, you can useselectNodes(args)METHOD. this method returnsListAnd the remaining operations are similar to dom4j.

At the same time, it also hasselectSingleNode(args)Returns a single Node.

Next we will continue to introduce other xpath syntaxes:

2. if the path is a double slash//It indicates that all of//Elements of subsequent rules (regardless of hierarchical relationships)

(1)//BBBWhich indicates selecting all BBB elements

    
 Here
     
 Here
         
  Here
     
         
              
   Here
   Here
      
 

(2)//DDD/BBBIndicates that all parent elements are the BBB elements of DDD.

    
     
     
     
         
  Here
     
         
              
   Here
   Here
      
 

3. asterisks*Select all elements located in the path prior to the asterisk

(1)/AAA/CCC/DDD/*It indicates that all elements with paths attached to/AAA/CCC/DDD are selected:

    
         
              
               
               
               
           
      
     
         
              
   Here
   Here
   Here
   Here
      
     
         
              
                   
                
           
      
 

(2)/*/*/*/BBBIt indicates all BBB elements with three ancestor elements

    
         
              
   Here
   Here
               
           
      
     
         
              
   Here
   Here
               
           
      
     
         
  
   
Here
                   
                
           
      
 

(3)//*It indicates selecting all elements

4. the expression in square brackets can further specify the element. The number indicates the position of the element in the selection set, while the last () function indicates the last element in the selection set. Note that the subscript starts from 1, not 0!
(1)/AAA/BBB[1]Which indicates selecting the first BBB sub-element of AAA

    
 This
     
     
 

(2)/AAA/BBB[last()]To select the last BBB element of AAA.

    
     
     
     
 This

5. operations on attributes

(1)//@idSelect all id attributes. note: all id attributes are returned as nodes, rather than nodes with id attributes.

    
 Return the id attribute node here.
 The id attribute node here is also returned.
     
 

(2)//BBB[@id], Select all BBB nodes with the id attribute

    
 Return this BBB node
 This BBB node is also returned.
     
 

(3)//BBB[@name]Select all BBB nodes with the name attribute

    
     
     
 Return this BBB node
 

(4)//BBB[@*], Select all BBB nodes with attributes

    
 Return this BBB node
 Return this BBB node
 Return this BBB node
 

(5)//BBB[not(@*)], Select all BBB nodes with no attribute

    
     
     
     
 This

6. attribute values can be used as selection criteria.

(1)//BBB[@id='b1'], Select the BBB element that contains the property id and its value is 'b1 '.

    
 This
     
 

7.count()The function can count the number of selected elements.

(1)//*[count(BBB)=2], Select an element that contains two BBB sub-elements

    
         
          
          
      
     
 
  
Returns this element.
          
      
     
         
          
      
 

(2)//*[count(*)=2], Select an element containing two child elements

    
         
          
          
      
     
 
  
Returns this element.
          
      
     
 
  
This element is also returned.
          
      
 

There are many other syntaxes, including many function applications, which are rarely used. we will not introduce them here.

In addition, the syntaxes described above can be combined as needed, such as the following xml documents:

    
         
              
   
    
K1
           
          
              
   
    
K2
   This
      
     
     
 

If we are looking for the KKK sub-element of the 2CCC sub-element under the 1st BBB sub-elements under the AAA element, the path of the xpath should be written as follows:
/AAA/BBB[1]/CCC[2]/KKK

Why xpath?

When using dom4j, we cannot obtain an element across layers. it must be obtained at one layer, which is very troublesome.
Therefore, we can use the xpath technology to access a node more conveniently, which allows us to conveniently read the specified node.

Xpath is usually used in combination with dom4j, and if you want to use xpath, you need to introduce a new package jaxen-1.1-beta-6.jar

The basic syntax of xpath includes the following:

1. The basic xpath syntax is similar to locating a file in a file system./The path indicates the absolute path to an element.

(1)/AAAIt indicates selecting the root element AAA

Here
     
     
     
     
         
      
      
  Here
 

(2)/AAA/CCCTo select all CCC sub-elements of AAA

    
     
 Here
     
     
         
      
      
  Here
 

(3)/AAA/DDD/BBBTo select all BBB sub-elements of the sub-element DDD of AAA.

    
     
     
     
     
         
  Here
      
  
 

So how can we use xpath in dom4j? It is actually very simple:

// 1. get the SAXReader parser SAXReader saxReader = new SAXReader (); // 2. specifies the file to be parsed, Document document = saxReader. read (new File (path); // 3. you can use xpath to read // document at will. selectNodes (args) returns multiple elements // document. selectSingleNode (args) returns a single element List nodes = document. selectNodes ("/AAA/BBB ");

After obtaining the document Object through dom4j, you can useselectNodes(args)METHOD. this method returnsListAnd the remaining operations are similar to dom4j.

At the same time, it also hasselectSingleNode(args)Returns a single Node.

Next we will continue to introduce other xpath syntaxes:

2. if the path is a double slash//It indicates that all of//Elements of subsequent rules (regardless of hierarchical relationships)

(1)//BBBWhich indicates selecting all BBB elements

    
 Here
     
 Here
         
  Here
     
         
              
   Here
   Here
      
 

(2)//DDD/BBBIndicates that all parent elements are the BBB elements of DDD.

    
     
     
     
         
  Here
     
         
              
   Here
   Here
      
 

3. asterisks*Select all elements located in the path prior to the asterisk

(1)/AAA/CCC/DDD/*It indicates that all elements with paths attached to/AAA/CCC/DDD are selected:

    
         
              
               
               
               
           
      
     
         
              
   Here
   Here
   Here
   Here
      
     
         
              
                   
                
           
      
 

(2)/*/*/*/BBBIt indicates all BBB elements with three ancestor elements

    
         
              
   Here
   Here
               
           
      
     
         
              
   Here
   Here
               
           
      
     
         
  
   
Here
                   
                
           
      
 

(3)//*It indicates selecting all elements

4. the expression in square brackets can further specify the element. The number indicates the position of the element in the selection set, while the last () function indicates the last element in the selection set. Note that the subscript starts from 1, not 0!
(1)/AAA/BBB[1]Which indicates selecting the first BBB sub-element of AAA

    
 This
     
     
 

(2)/AAA/BBB[last()]To select the last BBB element of AAA.

    
     
     
     
 This

5. operations on attributes

(1)//@idSelect all id attributes. note: all id attributes are returned as nodes, rather than nodes with id attributes.

    
 Return the id attribute node here.
 The id attribute node here is also returned.
     
 

(2)//BBB[@id], Select all BBB nodes with the id attribute

    
 Return this BBB node
 This BBB node is also returned.
     
 

(3)//BBB[@name]Select all BBB nodes with the name attribute

    
     
     
 Return this BBB node
 

(4)//BBB[@*], Select all BBB nodes with attributes

    
 Return this BBB node
 Return this BBB node
 Return this BBB node
 

(5)//BBB[not(@*)], Select all BBB nodes with no attribute

    
     
     
     
 This

6. attribute values can be used as selection criteria.

(1)//BBB[@id='b1'], Select the BBB element that contains the property id and its value is 'b1 '.

    
 This
     
 

7.count()The function can count the number of selected elements.

(1)//*[count(BBB)=2], Select an element that contains two BBB sub-elements

    
         
          
          
      
     
 
  
Returns this element.
          
      
     
         
          
      
 

(2)//*[count(*)=2], Select an element containing two child elements

    
         
          
          
      
     
 
  
Returns this element.
          
      
     
 
  
This element is also returned.
          
      
 

There are many other syntaxes, including many function applications, which are rarely used. we will not introduce them here.

In addition, the syntaxes described above can be combined as needed, such as the following xml documents:

    
         
              
   
    
K1
           
          
              
   
    
K2
   This
      
     
     
 

If we are looking for the KKK sub-element of the 2CCC sub-element under the 1st BBB sub-elements under the AAA element, the path of the xpath should be written as follows:
/AAA/BBB[1]/CCC[2]/KKK


The above is the content of XML--XPATH syntax introduction, more relevant content please pay attention to PHP Chinese network (www.php1.cn )!

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.