Python Reptile --Xpath (b)

Continued Previous: "Python Reptile --Xpath (a)."

3, Xpath shaft

Axis defines the relationship between the selected node tree with the current node.

Absolute location path, relative location path

Absolute location path starting at the forward slash "/", the path is not a relative position.

Step (step)

Location path comprises one or more steps, each step are divided slash: / step / step / ... (absolute position path), step / step / ... (the relative position of the path).
Step syntax: 轴名称::节点测试[谓语]
the set of nodes (axis name) used in the Xpath axis:

Axis name meaning
child Select all the child elements of the current node
parent Select the parent of the current node
descendant Select all descendants of the current node elements (descendants, etc.)
ancestor Select all the ancestors of the current node (grandfather, father, etc.)
descendant-or-self Select all descendants of the current node elements (descendants, etc.) as well as the current node itself
ancestor-or-self Select all the ancestors of the current node (grandfather, father, etc.) as well as the current node itself
preceding-sibling Prior to the selection of the current node all of the peer nodes
following-sibling All siblings after the current node selection
preceding All nodes before the start tag of the current node selected document
following All nodes after the end of the tab, select the document in the current node
self Select the current node
attribute Select all attributes of the current node
namespace Select all the namespace of the current node

Let's look at an example:

<xml version="1.0" encoding="ISO-8859-1">
<classroom>
    <student>
        <id>1001</id>
        <name lang="en">marry</name>
        <age>20</age>
        <country>China</country>
    </student>
    <student>
        <id>1002</id>
        <name lang="en">jack</name>
        <age>25</age>
        <country>USA</country>
    </student>
    <teacher>
        <classid>1<classid>
        <name lang="en">tom</name>
        <age>50</age>
        <country>USA</country>
    </teacher>
</classroom>
Achieve results Path expression
Child elements currently selected node teacher node calssroom /classroom/child::teacher
Select all the parent node id //id/parent::*
Select all ancestor nodes as child nodes classid //classid/ancestor::*
Select all descendants of nodes in the classroom node /classroom/descendant::*
All the student to select parent node id element //student/descendant::id
Select all the elements classid ancestor node and itself //classid/ancestor-or-self::*
Select / classroom / student itself and all descendant elements /classroom/student/descendant-or-self::*
Select all siblings before / classroom / teacher, the result is all the student selected nodes /classroom/teacher/preceding-sibling::*
Select all siblings / classroom student after the second, the result is the selected node teacher /classroom/student[2]/following-sibling::*
Node (in addition to their ancestors), not only the student node, and child node before which all selected / classroom / teacher node /classroom/teacher/preceding::*
Select / classroom all the nodes after the second student, the teacher result is selected nodes and child nodes /classroom/student[2]/following::*
Student select node, no means alone, primarily used in conjunction with other axes, such as the ancestor-or-self, descendant-or-self //student/self::*
Select all the attributes / classroom / teacher / name node /classroom/teacher/name/attrubute::*

Guess you like

Origin www.cnblogs.com/lykxbg/p/12006001.html