XPath is a language for locating elements in an XML document, commonly used in XML, HTML file parsing, and easier to use than CSS selectors.
XML file minimum constituent unit:
-Element (Elements node)
-Attribute (attribute node)
-Text (texts)
-namespace (name space)
-processing-instruction (Command handling)
-Comment (note)
-Root (root node)
XPath expression format
XPath selects nodes through "path expressions", similar to traditional file systems in expressive form
The absolute path (absolute path) must be preceded by "/", followed by the root node, such as/step/step/...
The relative path (relative path) is written in addition to the absolute path, such as Step/step, which means "/" is not used.
Slash (/) as a separator inside the path
"/": Indicates select root node
"//": Indicates that a node is selected anywhere
"@": Indicates the selection of a property
NodeName (node name): Indicates that all child nodes of the node are selected
XPath Location method
<?xml version="1.0" encoding="iso-8859-1"?>< bookstore> <book> <title lang="Eng">harry potter</title > <price>29.99</price> </book> <book> <title lang=" Eng">learning xml</title> <price>39.95</price> </ Book></bookstore>
#Bookstore: Selects all child nodes of the bookstore element. #/bookstore: Select the root node bookstore, which is the absolute path notation. #Bookstore/book: Selects all book elements that belong to the bookstore child element, which is the relative path notation. #//book: Selects all book child elements, regardless of their position in the document. #Bookstore//book: Selects all book elements that belong to descendants of the bookstore element, regardless of where they are located under bookstore. #//@lang: Select All properties named Lang. #/bookstore/book[1]: Represents the first book child element of the selection bookstore. #/bookstore/book[last ()]: Indicates the last book child element of the selection bookstore. #/bookstore/book[last ()-1]: Represents the second-to-last book child element of the selection bookstore. #/bookstore/book[position () <3]: Represents the first two book child elements of the selection bookstore. #//title[@lang]: Indicates that all the title nodes with the lang attribute are selected. #//title[@lang = ' eng ']: Indicates that the value of the Select all lang attribute equals the title node of "Eng". #/bookstore/book[price]: Represents the book child element that selects Bookstore, and the selected book element must have the price child element. #/bookstore/book[price>35.00]: Represents the book child element that selects Bookstore, and the price child element value of the selected book element must be greater than 35. #/bookstore/book[price>35.00]/title: Indicates in example 14 result set, select Title Child element. #/bookstore/book/price[.>35.00]: Represents the price child element that selects "/bookstore/book" with a value greater than 35. #//book/title |//book/price: Indicates that both the title child element and the price child element of the book element are selected.
XPath of Python