---
title: "XPath 笔记"
author: "Kyll"
date: "2008-03-22T09:39:29Z"
tags: []
slug: "xpath"
draft: false
lastmod: "2008-03-22T09:39:29Z"
id: 174746
url: "https://kyll.iteye.com/blog/174746"
---

XPath 有 7 种节点：  
element（元素）  
attribute（属性）  
text（文本）  
namespace（命名空间）  
processing-instruction（处理说明）  
comment（注释）  
document (root) nodes（文档节点或根节点）

属性值或文本， 是原子值。

每个元素或属性有一个父。  
元素的子元素可以有 0 个， 1 个， 或者多个。  
拥有同样父元素的成为兄弟。  
元素的最顶级父， 称为祖先。  
元素的子元素， 子元素的子元素等等， 称为子孙。

路径实例：  
/bookstore/book\[1]  
/bookstore/book\[last()]  
/bookstore/book\[last()-1]  
/bookstore/book\[position()&lt;3]  
//title\[@lang]  
//title\[@lang='eng']  
/bookstore/book\[price&gt;35.00]  
/bookstore/book\[price&gt;35.00]/title  
/bookstore/*  
//*  
//title\[@\*]  
//book/title | //book/price  
//title | //price  
/bookstore/book/title | //price

绝对路径：  
/step/step/...  
相对路径：  
step/step/...

轴实例：  
child::book  
attribute::lang  
child::*  
attribute::*  
child::text()  
child::node()  
descendant::book  
ancestor::book  
ancestor-or-self::book  
child::\*/child::price

操作符：

 

**Operator** **Description** **Example** **Return value** | Computes two node-sets //book | //cd Returns a node-set with all book and cd elements + Addition 6 + 4 10 - Subtraction 6 - 4 2 * Multiplication

6 * 4

24 div Division 8 div 4 2 = Equal price=9.80 true if price is 9.80  
false if price is 9.90 != Not equal price!=9.80 true if price is 9.90  
false if price is 9.80 &lt; Less than price&lt;9.80 true if price is 9.00  
false if price is 9.80 &lt;= Less than or equal to price&lt;=9.80 true if price is 9.00  
false if price is 9.90 &gt; Greater than price&gt;9.80 true if price is 9.90  
false if price is 9.80 &gt;= Greater than or equal to price&gt;=9.80 true if price is 9.90  
false if price is 9.70 or or price=9.80 or price=9.70 true if price is 9.80  
false if price is 9.50 and and price&gt;9.00 and price&lt;9.90 true if price is 9.80  
false if price is 8.50 mod Modulus (division remainder) 5 mod 2 1