APath
一、node
expression | description |
node | select all children nodes of this node |
/ | select from root node |
// | select the document node from the current matched node,regardless of their position |
. | select current node |
.. | select the parent node of current node |
@ | select properties |
二、predicate
expression | implication |
/bookstore/book[1] | select the first book element belongs to bookstore |
/bookstore/book[last()] | select the lastbook element belongs to bookstore |
/bookstore/book[position()<3] | select the first two book elements belongs to bookstore |
//title[@lang] | choose elements of title with lang property |
三、wildcard
expression | description | usage | specification |
* | match any node | xpath("/div/*") | select all children nodes under div node |
@* | match nodes with any property | xpath("/div[@*]") | select all node with properties |
node() | match nodes of any type | xpath("//div[@class=\"tb_cls\"]").node() | select all div tags which class property is tb_cls |
四、multiple path selection
usage | specification |
xpath("//book/title*|*//book/price") | select all title elements and price elements of book |
xpath("//title|//price") | select all title elements and price elements in the document |
五、xpath axis
expression | description | usage | illustration |
ancestor | select all ancestor nodes of current node | xpath("//div[@id=\"123\"]/ancestor::*") | select all ancestor nodes that tags name is div and id property is 123 |
ancestor-or-self | select all ancestor node of current node and itself | xpath("//div[@id=\"123\"]/ancestor-or-self::*") | select all ancestor nodes and itself that tags name is div and id property is 123 |
attribute | select all attributs of current node | xpath("//div[@id=\"123\"]/attribute::class") | select class attribute name that tags name is div and id property is 123 |
child | select all child nodes of current node | xpath("//div[@id=\"123\"]/child::book") | select all child book nodes that tags name is div and id property is 123 |
descendant | ... | xpath("./descendant::*") | ... |
following | ... | xpath("./following::*") | ... |
parent | ... | xpath("./parent::*") | ... |
preceding | ... | xpath("//div[@id=\"123\"]/preceding::*") | ... |
preceding-sibling | ... | xpath("//div[@id=\"123\"]/preceding-sibling::*") | ... |
self | ... | xpath("./self::*") | ... |
六、operators
expression | description | usage | illustration |
+ | ... | ... | ... |
- | ... | ... | ... |
* | ... | ... | ... |
div | ... | ... | ... |
= | ... | ... | ... |
!= | ... | ... | ... |
< | ... | ... | ... |
<= | ... | ... | ... |
> |
... | ... | ... |
>= | ... | ... | ... |
or | ... | ... | ... |
and | ... | ... | ... |
mod | ... | ... | ... |
七、functions
expression | description | usage | illustration |
starts-with() | ... | xpath("//div[starts-with(@id,\"ma\")]") | ... |
contains() | ... | xpath("//div[contains(@id,\"ma\")]") | ... |
and | ... | xpath("//div[contains(@id,\"ma\")] and contains(@id,\"in\")") | ... |
text() | ... | xpath("//div[contains(text(),\"ma\")]") | ... |