Crazy XML learning notes (12) ------------ XPath

Source: Internet
Author: User
Tags xpath contains
XPath is a language used to search for information in XML documents. XPath is used to navigate through elements and attributes in XML documents. XPath is a language used to search for information in XML documents. XPath is used to navigate through elements and attributes in XML documents.

What is XPath?
  • XPath uses path expressions for navigation in XML documents

  • XPath contains a standard function library

  • XPath is the main element in XSLT.

  • XPath is a W3C standard

XPath path expression

XPath uses path expressions to select nodes or node sets in XML documents. These path expressions are very similar to the expressions we see in conventional computer file systems.

XPath standard functions

XPath contains more than 100 built-in functions. These functions are used for string value, numeric value, date and time comparison, node and QName processing, sequence processing, logical value, and so on.


In XPath, there are seven types of nodes: elements, attributes, text, namespaces, processing commands, comments, and document nodes (or become root nodes ).

XPath term Node)

There are seven types of nodes in XPath: elements, attributes, text, namespaces, processing commands, comments, and document (root) nodes. XML documents are treated as node trees. The root of a tree is called a document node or a root node.

See the following XML document:

 
 
    
   Harry Potter  J K. Rowling   
   
    2005
     
   
    29.99
   
  
 

Node example in the XML document above:

 
  
(Document Node) j k. Rowling(Element node) lang = "en" (attribute node)
 
Basic value (or Atomic value)

The basic value is a node with no parent or child.

Example of the basic value:

J K. Rowling"en"
Item)

A project is a basic value or node.

Parent node)

Each element and attribute has a parent.

In the following example, the book element is the parent of the title, author, year, and price elements:

   
  Harry Potter  J K. Rowling  
  
   2005
    
  
   29.99
  
 
Child)

Element nodes can have zero, one, or more sub-nodes.

In the following example, the title, author, year, and price elements are child of the book element:

   
  Harry Potter  J K. Rowling  
  
   2005
    
  
   29.99
  
 
Sibling)

Nodes with the same parent

In the following example, the title, author, year, and price elements are all siblings:

   
  Harry Potter  J K. Rowling  
  
   2005
    
  
   29.99
  
 
Advanced (Ancestor)

The parent and parent of a node.

In the following example, the first generation of the title element is the book element and the bookstore element:

 
    
   Harry Potter  J K. Rowling  
   
    2005
     
   
    29.99
   
  
 
Descendant (Descendant)

The child of a node, the child of the child, and so on.

In the following example, the future generations of bookstore are book, title, author, year, and price:

 
    
   Harry Potter  J K. Rowling  
   
    2005
     
   
    29.99
   
  
 


XPath Axes)

XML instance document

We will use this XML document in the following example:

 
 
    
   Harry Potter  
   
    29.99
   
  
    
   Learning XML  
   
    39.95
   
  
 

XPath axis

The axis defines a node set relative to the current node.

Axis name Result
Ancestor Select all the ancestors of the current node (parent, grandfather, etc)
Ancestor-or-self Select all the founders of the current node (parent, grandfather, etc.) and the current node itself
Attribute Select all attributes of the current node
Child Select all child elements of the current node.
Descendant Select all descendant elements (child, Sun, etc.) of the current node ).
Descendant-or-self Select all descendant elements (child, Sun, etc.) of the current node and the current node itself.
Following Select all nodes after the end label of the current node in the document.
Namespace Select all namespace nodes of the current node
Parent Select the parent node of the current node.
Preceding Select all nodes before the start label of the current node in the document.
Preceding-sibling Select all peer nodes before the current node.
Self Select the current node.

Location path expression

The location path can be absolute or relative.

The absolute path starts with a forward slash (/), but the relative path does not. In either case, the path contains one or more steps, and each step is separated by a slash:

Absolute path:
/step/step/...
Relative path:
step/step/...

Each step is calculated based on the nodes in the current node set.

Steps include:
  • Axis)

  • Define the tree relationship between the selected node and the current node

  • Node-test)

  • Recognizes nodes inside a certain axis

  • Zero or more predicates (predicate)

  • Further refine the selected node set

Step syntax:
Axis name: node test [predicates]
Instance
Example Result
Child: book Select the book nodes of all the child elements of the current node
Attribute: lang Select the lang attribute of the current node
Child ::* Select all child elements of the current node
Attribute ::* Select all attributes of the current node
Child: text () Select all text subnodes of the current node
Child: node () Select all subnodes of the current node
Descendant: book Select all book descendants of the current node
Ancestor: book Select all book predecessors of the current node
Ancestor-or-self: book Select all the book predecessors of the current node and the current node (if this node is a book node)
Child: */child: price Select all price grandchildren of the current node.

XPath operators


XPath expressions can return node sets, strings, logical values, and numbers.

XPath operators

Operators available in XPath expressions are listed below:

Operator Description Instance Return value
| Calculate two node sets // Book | // cd Returns all node sets with book and cd elements.
+ Addition 6 + 4 10
- Subtraction 6-4 2
* Multiplication 6*4 24
P Division 8 p 4 2
= Equal Price = 9.80

If the price is 9.80, true is returned.

If the price is 9.90, return fasle.

! = Not equal Price! = 9.80

If the price is 9.90, true is returned.

If the price is 9.80, return fasle.

< Less Price <1, 9.80

If the price is 9.00, true is returned.

If the price is 9.90, return fasle.

<= Less than or equal Price <= 9.80

If the price is 9.00, true is returned.

If the price is 9.90, return fasle.

> Greater Price> 9.80

If the price is 9.90, true is returned.

If the price is 9.80, return fasle.

> = Greater than or equal Price >=9.80

If the price is 9.90, true is returned.

If the price is 9.70, return fasle.

Or Or Price = 9.80 or price = 9.70

If the price is 9.80, true is returned.

If the price is 9.50, return fasle.

And And Price> 9.00 and price <9.90

If the price is 9.80, true is returned.

If the price is 8.50, return fasle.

Mod Calculate the remainder of the division 5 mod 2 1

XML instance document

We will use this XML document in the following example:

"Books. xml ":
 
 
    
   Everyday Italian  Giada De Laurentiis  
   
    2005
     
   
    30.00
   
  
    
   Harry Potter  J K. Rowling  
   
    2005
     
   
    29.99
   
  
    
   XQuery Kick Start  James McGovern  Per Bothner  Kurt Cagle  James Linn  Vaidyanathan Nagarajan  
   
    2003
     
   
    49.99
   
  
    
   Learning XML  Erik T. Ray  
   
    2003
     
   
    39.95
   
  
 

Node selection

We will use Microsoft's xml dom object to load the XML document, and use the selectNodes () function to select nodes from the XML document:

Set xmlDoc = CreateObject ("Microsoft. XMLDOM") xmlDoc. async = "false" xmlDoc. load ("books. xml") xmlDoc. selectNodes (path expression)

Select all book nodes

The following example selects all the book nodes under the bookstore element:

xmlDoc.selectNodes("/bookstore/book")

Select the first book node

In the following example, only the first book node under the bookstore element is selected:

xmlDoc.selectNodes("/bookstore/book[0]")

Select price

The following example selects text from all price nodes:

xmlDoc.selectNodes("/bookstore/book/price/text()")

Select a price above 35

The following example selects all price nodes with prices higher than 35:

xmlDoc.selectNodes("/bookstore/book[price>35]/price")

Select a title node with a price higher than 35

The following example selects all title nodes with prices higher than 35:

xmlDoc.selectNodes("/bookstore/book[price>35]/title")

The above is the content of crazy XML learning notes (12) ------------ XPath. For more information, see PHP Chinese website (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.