XPath axis positioning (and shaft step)

XPath axes (Axes)

Shaft means relative to the set of nodes of the current node

Axis name result
ancestor Select all ancestors of the current node (parent, grandparent, etc.)
ancestor-or-self Select all the ancestors of the current node (parent, grandparent, etc.) as well as the current node itself
attribute Select all the attributes of the current node. @id equivalent to attribute :: id
child Select all child elements of the current node, title equivalent to child: title
descendant Select all the elements of the current node descendant (son, Sun, etc.)
descendant-or-self Select all descendants of the current node's operation (sub, Sun, etc.) and the current node itself
following All nodes after the end of the tab, select the document in the current node
namespace Select all the namespace of the current node
parent Select the parent of the current node
preceding Select the parent of the current node
preceding-sibling Prior to the selection of the current node all of the peer nodes
self Select the current node. And other control self :: node ()

Example:

example meaning
parent::div Upper parent node, you called biological father div, there is at most one;
child::div Underlying all child nodes, all your own child named in the div;
ancestor::div Above all immediate node, you are my father biological father or your own father or your own father called in the div;
descendant::div All of the following nodes, your offspring called div, not including the offspring for your brother;
following::div Since all nodes in the following pages you call the div;
following-sibling::div Under the same layer node, all your younger brother in a div called;
preceding::div On the same layer node, all of your elder brother and their descendants called the div;
preceding-sibling::div On the same layer node, all of your elder brother in a div called;

Step Step

Syntax axis names steps: node test [predicate]

example result
child::book Select the book belongs to all nodes of the current node only elements
attribute :: long Select lang attribute of the current node
child::* Select all the elements of the current node only
attribute::* Select all attributes of the current node
child::text() Select all the text of the current node child nodes
child::node() Select all child nodes of the current node
descendant::book Select all book descendants of the current node
ancestor:book Select all book ancestors of the current node
ancestor-or-self::book Select all book ancestors of the current node and the current node (if the node is a book node)
child::*/child::price Select all of the current node price Sun

Examples

//a/parent::h3

Here Insert Picture Description

//div[@price<100]/child::div

Here Insert Picture Description
//div[@price<100]/descendant::h2

Here Insert Picture Description
//a/ancestor::div

Here Insert Picture Description

//div[@id="testid"][2]/following::div
Here Insert Picture Description
//div[@id="testid"][2]/following::li
Here Insert Picture Description

//div[@id="testid"][2]/following-sibling::div[@id="go"]
Here Insert Picture Description

//div[@id="testid"]/preceding::ul
Here Insert Picture Description
//div[@id="testid"][2]/preceding-sibling::div

Here Insert Picture Description

//a/attribute::href
Here Insert Picture Description

//h3/child::*
Select all the elements of the current node only
Here Insert Picture Description

All exercises code: https://github.com/SaltNego/Web_Crawler_Notes

Published 61 original articles · won praise 22 · views 4235

Guess you like

Origin blog.csdn.net/yiqiushi4748/article/details/104037528