语法
| 表达式 | 
描述 | 
nodename | 
选取此节点的所有子节点 | 
/ | 
从根节点选取 | 
// | 
选择所有符合条件的节点,不管位置 | 
. | 
选择当前节点 | 
.. | 
当前节点的父节点 | 
@ | 
选取属性 | 
谓语
| 路径表达式 | 
结果 | 
/bookstore/book[1] | 
选取属于 bookstore 子元素的第一个 book 元素。 | 
/bookstore/book[last()] | 
选取属于 bookstore 子元素的最后一个 book 元素。 | 
/bookstore/book[last()-1] | 
选取属于 bookstore 子元素的倒数第二个 book 元素。 | 
/bookstore/book[position()<3] | 
选取最前面的两个属于 bookstore 元素的子元素的 book 元素。 | 
//title[@lang] | 
选取所有拥有名为 lang 的属性的 title 元素。 | 
//title[@lang='eng'] | 
选取所有 title 元素,且这些元素拥有值为 eng 的 lang 属性。 | 
/bookstore/book[price>35.00] | 
选取 bookstore 元素的所有 book 元素,且其中的 price 元素的值须大于 35.00 。 | 
/bookstore/book[price>35.00]/title | 
选取 bookstore 元素中的 book 元素的所有 title 元素,且其中的 price 元素的值须大于 35.00 。 | 
选取若干路径
| 路径表达式 | 
结果 | 
//book/title | //book/price | 
选取 book 元素的所有 title 和 price 元素。 | 
//title | //price | 
选取文档中的所有 title 和 price 元素。 | 
/bookstore/book/title | //price | 
选取属于 bookstore 元素的 book 元素的所有 title 元素,以及文档中所有的 price 元素。 | 
轴
| 轴名称 | 
结果 | 
ancestor | 
选取当前节点的所有先辈(父、祖父等)。 | 
ancestor-or-self | 
选取当前节点的所有先辈(父、祖父等)以及当前节点本身。 | 
attribute | 
选取当前节点的所有属性。 | 
child | 
选取当前节点的所有子元素。 | 
descendant | 
选取当前节点的所有后代元素(子、孙等)。 | 
descendant-or-self | 
选取当前节点的所有后代元素(子、孙等)以及当前节点本身。 | 
following | 
选取文档中当前节点的结束标签之后的所有节点。 | 
following-sibling | 
选取当前节点之后的所有兄弟节点 | 
namespace | 
选取当前节点的所有命名空间节点。 | 
parent | 
选取当前节点的父节点。 | 
preceding | 
选取文档中当前节点的开始标签之前的所有节点。 | 
preceding-sibling | 
选取当前节点之前的所有同级节点。 | 
self | 
选取当前节点。 | 
运算符
| 运算符 | 
描述 | 
实例 | 
返回值 | 
| ` | 
` | 
计算两个节点集 | 
//book | 
+ | 
加法 | 
6 + 4 | 
10 | 
- | 
减法 | 
6 - 4 | 
2 | 
* | 
乘法 | 
6 * 4 | 
24 | 
div | 
除法 | 
8 div 4 | 
2 | 
= | 
等于 | 
price=9.80 | 
如果 price 是 9.80,则返回 true。如果 price 是 9.90,则返回 false。 | 
!= | 
不等于 | 
price!=9.80 | 
如果 price 是 9.90,则返回 true。如果 price 是 9.80,则返回 false。 | 
< | 
小于 | 
price<9.80 | 
如果 price 是 9.00,则返回 true。如果 price 是 9.90,则返回 false。 | 
<= | 
小于或等于 | 
price<=9.80 | 
如果 price 是 9.00,则返回 true。如果 price 是 9.90,则返回 false。 | 
> | 
大于 | 
price>9.80 | 
如果 price 是 9.90,则返回 true。 如果 price 是 9.80,则返回 false。 | 
>= | 
大于或等于 | 
price>=9.80 | 
如果 price 是 9.90,则返回 true。如果 price 是 9.70,则返回 false。 | 
or | 
或 | 
price=9.80 or price=9.70 | 
如果 price 是 9.80,则返回 true。如果 price 是 9.50,则返回 false。 | 
and | 
与 | 
price>9.00 and price<9.90 | 
如果 price 是 9.80,则返回 true。如果 price 是 8.50,则返回 false。 | 
not | 
取反 | 
not(true) | 
false | 
mod | 
计算除法的余数 | 
5 mod 2 | 
1 | 
函数
| 函数名 | 
描述 | 
count() | 
计数 | 
concat(str,str,...) | 
字符串拼接 | 
string-length(str) | 
返回 str 的长度 | 
local-name(node) | 
解析 node 节点的名称 | 
string(str) | 
返回 str 字符串 | 
substring(string,start,len) \n fn:substring(string,start) | 
返回从 start 位置开始的指定长度的子字符串。第一个字符的下标是 1。如果省略 len 参数,则返回从位置 start 到字符串末尾的子字符串。 | 
translate(string1,string2,string3) | 
把 string1 中的 string2 替换为 string3。 | 
contains(string1,string2) | 
如果 string1 包含 string2,则返回 true,否则返回 false。 | 
starts-with(string1,string2) | 
如果 string1 以 string2 开始,则返回 true,否则返回 false | 
substring-before(string1,string2) | 
返回 string2 在 string1 中出现之前的子字符串。 | 
substring-after(string1,string2) | 
返回 string2 在 string1 中出现之后的子字符串。 | 
lxml中没有ends-with,可以使用下面的方法
1
   | substring(@class, string-length(@class) - strting-length('-wrapper') +1 ) = '-wrapper'
  | 
 
将 Element 对象转为 str
1 2 3
   | from lxml import etree html = etree.HTML(txt) txt = etree.tostring(html)
   |