Teacher Dana's Python Notes
Xpath
- A set of rules/language for finding information in XML files, traversing based on XML elements or attributes, and based on XML elements or attributes.
Look at an XML file
reference
- http://www.w3school.com.cn/xpath/index.asp
XPath development tools
- Open source XPath expression editing tool: XMLQUire
- Chrome plugin: XPath Helper
- Firefox add-on: XPath Checker
Select node
- nodename: select all child nodes of this node
- /: Select from the root node
- /Student: Find a node called Student from the root node. Look at the above picture, only the School node.
- //: Select node, regardless of location
- //age: select three nodes, generally form a list and return
- .: Select the current node
- (Two points): select the parent node of the current node
- @: select attribute
- Search in xpath is generally searched by path method (/ means son, // means offspring)
- School/Teacher: Return to Teacher node
- School/Student: return two Student nodes
- //Student: select all Student nodes, regardless of location
- School//Age: select all Age nodes in School
- //@other: select other attribute
- //Age[@Detail]: select Age element with attribute Detail
Predicates-Predicates
- /School/Student[1]: Select the first Student node under School
- /School/Student[last()]: select the last Student node under School
- /School/Student[last()-1]: Select the second-to-last Student node under School
- /School/Student[position()< 3]: Select the first two Student nodes under School
- //Student[@score]: select the Student node with the attribute score
- //Student[@score=“99”]: Select the Student node with the attribute score and the attribute value is 99
- //Student[@score]/Age: select the child node Age of the Student node with the attribute score
Xpath some operations
- |: Or
- //Student[@score] | //Teacher: select Student node and Teacher node with attribute score
- Other uncommon Xpath operation symbols include +, -, *, div, >, <
end! ! !