XPath最通俗的教程(ZZ)

  以下是本人找到的最完整最易懂的XPath教程,不敢私藏,拿出来与大家分享。帮我点旁边的google广告呀。

实例 1
基本的XPath语法类似于在一个文件系统中定位文件,如果路径以斜线 / 开始, 那么该路径就表示到一个元素的绝对路径

/AAA

选择根元素AAA


     <
AAA
          <BBB/> 
          <CCC/> 
          <BBB/> 
          <BBB/> 
          <DDD> 
               <BBB/> 
          </DDD> 
          <CCC/> 
     </
AAA>

/AAA/CCC

选择AAA的所有CCC子元素


     <AAA> 
          <BBB/> 
          <
CCC/> 
          <BBB/> 
          <BBB/> 
          <DDD> 
               <BBB/> 
          </DDD> 
          <
CCC/> 
     </AAA>

/AAA/DDD/BBB

选择AAA的子元素DDD的所有子元素


     <AAA> 
          <BBB/> 
          <CCC/> 
          <BBB/> 
          <BBB/> 
          <DDD> 
               <
BBB/> 
          </DDD> 
          <CCC/> 
     </AAA>

实例 2

如果路径以双斜线 // 开头, 则表示选择文档中所有满足双斜线//之后规则的元素(无论层级关系)

//BBB

选择所有BBB元素


     <AAA> 
          <
BBB/> 
          <CCC/> 
          <
BBB/> 
          <DDD> 
               <
BBB/> 
          </DDD> 
          <CCC> 
               <DDD> 
                    <
BBB/> 
                    <
BBB/> 
               </DDD> 
          </CCC> 
     </AAA>

//DDD/BBB

选择所有父元素是DDD的BBB元素


     <AAA> 
          <BBB/> 
          <CCC/> 
          <BBB/> 
          <DDD> 
               <
BBB/> 
          </DDD> 
          <CCC> 
               <DDD> 
                    <
BBB/> 
                    <
BBB/> 
               </DDD> 
          </CCC> 
     </AAA>

实例 3

星号 * 表示选择所有由星号之前的路径所定位的元素

/AAA/CCC/DDD/*

选择所有路径依附于/AAA/CCC/DDD的元素


     <AAA> 
          <XXX> 
               <DDD> 
                    <BBB/> 
                    <BBB/> 
                    <EEE/> 
                    <FFF/> 
               </DDD> 
          </XXX> 
          <CCC> 
               <DDD> 
                    <
BBB/> 
                    <
BBB/> 
                    <
EEE/> 
                    <
FFF/> 
               </DDD> 
          </CCC> 
          <CCC> 
               <BBB> 
                    <BBB> 
                         <BBB/> 
                    </BBB> 
               </BBB> 
          </CCC> 
     </AAA>

/*/*/*/BBB

选择所有的有3个祖先元素的BBB元素


     <AAA> 
          <XXX> 
               <DDD> 
                    <
BBB/> 
                    <
BBB/> 
                    <EEE/> 
                    <FFF/> 
               </DDD> 
          </XXX> 
          <CCC> 
               <DDD> 
                    <
BBB/> 
                    <
BBB/> 
                    <EEE/> 
                    <FFF/> 
               </DDD> 
          </CCC> 
          <CCC> 
               <BBB> 
                    <
BBB
                         <BBB/> 
                    </
BBB
               </BBB> 
          </CCC> 
     </AAA>

//*

选择所有元素


     <
AAA
          <
XXX
               <
DDD
                    <
BBB/> 
                    <
BBB/> 
                    <
EEE/> 
                    <
FFF/> 
               </
DDD
          </
XXX
          <
CCC
               <
DDD
                    <
BBB/> 
                    <
BBB/> 
                    <
EEE/> 
                    <
FFF/> 
               </
DDD
          </
CCC
          <
CCC
               <
BBB
                    <
BBB
                         <
BBB/> 
                    </
BBB
               </
BBB
          </
CCC
     </
AAA>

实例 4

方块号里的表达式可以进一步的指定元素, 其中数字表示元素在选择集里的位置, 而last()函数则表示选择集中的最后一个元素.

/AAA/BBB[1]

选择AAA的第一个BBB子元素


     <AAA> 
          <
BBB/> 
          <BBB/> 
          <BBB/> 
          <BBB/> 
     </AAA>

/AAA/BBB[last()]

选择AAA的最后一个BBB子元素


     <AAA> 
          <BBB/> 
          <BBB/> 
          <BBB/> 
          <
BBB/> 
     </AAA>

实例 5

//@id

选择所有的id属性


     <AAA> 
          <BBB
 id = "b1"/> 
          <BBB
 id = "b2"/> 
          <BBB name = "bbb"/> 
          <BBB/> 
     </AAA>

//BBB[@id]

选择有id属性的BBB元素


     <AAA> 
          <
BBB id = "b1"/> 
          <
BBB id = "b2"/> 
          <BBB name = "bbb"/> 
          <BBB/> 
     </AAA>

//BBB[@name]

选择有name属性的BBB元素


     <AAA> 
          <BBB id = "b1"/> 
          <BBB id = "b2"/> 
          <
BBB name = "bbb"/> 
          <BBB/> 
     </AAA>

//BBB[@*]

选择有任意属性的BBB元素


     <AAA> 
          <
BBB id = "b1"/> 
          <
BBB id = "b2"/> 
          <
BBB name = "bbb"/> 
          <BBB/> 
     </AAA>

//BBB[not(@*)]

选择没有属性的BBB元素


     <AAA> 
          <BBB id = "b1"/> 
          <BBB id = "b2"/> 
          <BBB name = "bbb"/> 
          <
BBB/> 
     </AAA>

实例 6

属性的值可以被用来作为选择的准则, normalize-space函数删除了前部和尾部的空格, 并且把连续的空格串替换为一个单一的空格

//BBB[@id='b1']

选择含有属性id且其值为'b1'的BBB元素


     <AAA> 
          <
BBB id = "b1"/> 
          <BBB name = " bbb "/> 
          <BBB name = "bbb"/> 
     </AAA>

//BBB[@name='bbb']

选择含有属性name且其值为'bbb'的BBB元素


     <AAA> 
          <BBB id = "b1"/> 
          <BBB name = " bbb "/> 
          <
BBB name = "bbb"/> 
     </AAA>

//BBB[normalize-space(@name)='bbb']

选择含有属性name且其值(在用normalize-space函数去掉前后空格后)为'bbb'的BBB元素


     <AAA> 
          <BBB id = "b1"/> 
          <
BBB name = " bbb "/> 
          <
BBB name = "bbb"/> 
     </AAA>

实例 7

count()函数可以计数所选元素的个数

//*[count(BBB)=2]

选择含有2个BBB子元素的元素


     <AAA> 
          <CCC> 
               <BBB/> 
               <BBB/> 
               <BBB/> 
          </CCC> 
          <
DDD
               <BBB/> 
               <BBB/> 
          </
DDD
          <EEE> 
               <CCC/> 
               <DDD/> 
          </EEE> 
     </AAA>

//*[count(*)=2]

选择含有2个子元素的元素


     <AAA> 
          <CCC> 
               <BBB/> 
               <BBB/> 
               <BBB/> 
          </CCC> 
          <
DDD
               <BBB/> 
               <BBB/> 
          </
DDD
          <
EEE
               <CCC/> 
               <DDD/> 
          </
EEE
     </AAA>

//*[count(*)=3]

选择含有3个子元素的元素


     <
AAA
          <
CCC
               <BBB/> 
               <BBB/> 
               <BBB/> 
          </
CCC
          <DDD> 
               <BBB/> 
               <BBB/> 
          </DDD> 
          <EEE> 
               <CCC/> 
               <DDD/> 
          </EEE> 
     </
AAA>

实例 8

name()函数返回元素的名称, start-with()函数在该函数的第一个参数字符串是以第二个参数字符开始的情况返回true, contains()函数当其第一个字符串参数包含有第二个字符串参数时返回true.

//*[name()='BBB']

选择所有名称为BBB的元素(这里等价于//BBB)


     <AAA> 
          <BCC> 
               <
BBB/> 
               <
BBB/> 
               <
BBB/> 
          </BCC> 
          <DDB> 
               <
BBB/> 
               <
BBB/> 
          </DDB> 
          <BEC> 
               <CCC/> 
               <DBD/> 
          </BEC> 
     </AAA>

//*[starts-with(name(),'B')]

选择所有名称以"B"起始的元素


     <AAA> 
          <
BCC
               <
BBB/> 
               <
BBB/> 
               <
BBB/> 
          </
BCC
          <DDB> 
               <
BBB/> 
               <
BBB/> 
          </DDB> 
          <
BEC
               <CCC/> 
               <DBD/> 
          </
BEC
     </AAA>

//*[contains(name(),'C')]

选择所有名称包含"C"的元素


     <AAA> 
          <
BCC
               <BBB/> 
               <BBB/> 
               <BBB/> 
          </
BCC
          <DDB> 
               <BBB/> 
               <BBB/> 
          </DDB> 
          <
BEC
               <
CCC/> 
               <DBD/> 
          </
BEC
     </AAA>

实例 10

多个路径可以用分隔符 | 合并在一起

//CCC | //BBB

选择所有的CCC和BBB元素


     <AAA> 
          <
BBB/> 
          <
CCC/> 
          <DDD> 
               <
CCC/> 
          </DDD> 
          <EEE/> 
     </AAA>

/AAA/EEE | //BBB

选择所有的BBB元素和所有是AAA的子元素的EEE元素


     <AAA> 
          <
BBB/> 
          <CCC/> 
          <DDD> 
               <CCC/> 
          </DDD> 
          <
EEE/> 
     </AAA>

/AAA/EEE | //DDD/CCC | /AAA | //BBB

可以合并的路径数目没有限制


     <
AAA
          <
BBB/> 
          <CCC/> 
          <DDD> 
               <
CCC/> 
          </DDD> 
          <
EEE/> 
     </
AAA>

实例 11

child轴(axis)包含上下文节点的子元素, 作为默认的轴,可以忽略不写.

/AAA

等价于 /child::AAA


     <
AAA
          <BBB/> 
          <CCC/> 
     </
AAA>

/child::AAA

等价于/AAA


     <
AAA
          <BBB/> 
          <CCC/> 
     </
AAA>

/AAA/BBB

等价于/child::AAA/child::BBB


     <AAA> 
          <
BBB/> 
          <CCC/> 
     </AAA>

/child::AAA/child::BBB

等价于/AAA/BBB


     <AAA> 
          <
BBB/> 
          <CCC/> 
     </AAA>

/child::AAA/BBB

二者都可以被合并


     <AAA> 
          <
BBB/> 
          <CCC/> 
     </AAA>

 

 

 

 

实例 12

descendant (后代)轴包含上下文节点的后代,一个后代是指子节点或者子节点的子节点等等, 因此descendant轴不会包含属性和命名空间节点.

/descendant::*

选择文档根元素的所有后代.即所有的元素被选择


     <
AAA
          <
BBB
               <
DDD
                    <
CCC
                         <
DDD/> 
                         <
EEE/> 
                    </
CCC
               </
DDD
          </
BBB
          <
CCC
               <
DDD
                    <
EEE
                         <
DDD
                              <
FFF/> 
                         </
DDD
                    </
EEE
               </
DDD
          </
CCC
     </
AAA>

/AAA/BBB/descendant::*

选择/AAA/BBB的所有后代元素


     <AAA> 
          <BBB> 
               <
DDD
                    <
CCC
                         <
DDD/> 
                         <
EEE/> 
                    </
CCC
               </
DDD
          </BBB> 
          <CCC> 
               <DDD> 
                    <EEE> 
                         <DDD> 
                              <FFF/> 
                         </DDD> 
                    </EEE> 
               </DDD> 
          </CCC> 
     </AAA>

//CCC/descendant::*

选择在祖先元素中有CCC的所有元素


     <AAA> 
          <BBB> 
               <DDD> 
                    <CCC> 
                         <
DDD/> 
                         <
EEE/> 
                    </CCC> 
               </DDD> 
          </BBB> 
          <CCC> 
               <
DDD
                    <
EEE
                         <
DDD
                              <
FFF/> 
                         </
DDD
                    </
EEE
               </
DDD
          </CCC> 
     </AAA>

//CCC/descendant::DDD

选择所有以CCC为祖先元素的DDD元素


     <AAA> 
          <BBB> 
               <DDD> 
                    <CCC> 
                         <
DDD/> 
                         <EEE/> 
                    </CCC> 
               </DDD> 
          </BBB> 
          <CCC> 
               <
DDD
                    <EEE> 
                         <
DDD
                              <FFF/> 
                         </
DDD
                    </EEE> 
               </
DDD
          </CCC> 
     </AAA>

 

 

 

 

实例 13

parent轴(axis)包含上下文节点的父节点, 如果有父节点的话

//DDD/parent::*

选择DDD元素的所有父节点


     <AAA> 
          <
BBB
               <DDD> 
                    <
CCC
                         <DDD/> 
                         <EEE/> 
                    </
CCC
               </DDD> 
          </
BBB
          <
CCC
               <DDD> 
                    <
EEE
                         <DDD> 
                              <FFF/> 
                         </DDD> 
                    </
EEE
               </DDD> 
          </
CCC
     </AAA>

 

 

 

 

实例 14

ancestor轴(axis)包含上下节点的祖先节点, 该祖先节点由其上下文节点的父节点以及父节点的父节点等等诸如此类的节点构成,所以ancestor轴总是包含有根节点,除非上下文节点就是根节点本身.

/AAA/BBB/DDD/CCC/EEE/ancestor::*

选择一个绝对路径上的所有节点


     <
AAA
          <
BBB
               <
DDD
                    <
CCC
                         <DDD/> 
                         <EEE/> 
                    </
CCC
               </
DDD
          </
BBB
          <CCC> 
               <DDD> 
                    <EEE> 
                         <DDD> 
                              <FFF/> 
                         </DDD> 
                    </EEE> 
               </DDD> 
          </CCC> 
     </
AAA>

//FFF/ancestor::*

选择FFF元素的祖先节点


     <
AAA
          <BBB> 
               <DDD> 
                    <CCC> 
                         <DDD/> 
                         <EEE/> 
                    </CCC> 
               </DDD> 
          </BBB> 
          <
CCC
               <
DDD
                    <
EEE
                         <
DDD
                              <FFF/> 
                         </
DDD
                    </
EEE
               </
DDD
          </
CCC
     </
AAA>

 

 

 

 

实例 15

following-sibling轴(axis)包含上下文节点之后的所有兄弟节点

/AAA/BBB/following-sibling::*


     <AAA> 
          <BBB> 
               <CCC/> 
               <DDD/> 
          </BBB> 
          <
XXX
               <DDD> 
                    <EEE/> 
                    <DDD/> 
                    <CCC/> 
                    <FFF/> 
                    <FFF> 
                         <GGG/> 
                    </FFF> 
               </DDD> 
          </
XXX
          <
CCC
               <DDD/> 
          </
CCC
     </AAA>

//CCC/following-sibling::*


     <AAA> 
          <BBB> 
               <CCC/> 
               <
DDD/> 
          </BBB> 
          <XXX> 
               <DDD> 
                    <EEE/> 
                    <DDD/> 
                    <CCC/> 
                    <
FFF/> 
                    <
FFF
                         <GGG/> 
                    </
FFF
               </DDD> 
          </XXX> 
          <CCC> 
               <DDD/> 
          </CCC> 
     </AAA>

 

 

 

 

实例 16

preceding-sibling 轴(axis)包含上下文节点之前的所有兄弟节点

/AAA/XXX/preceding-sibling::*


     <AAA> 
          <
BBB
               <CCC/> 
               <DDD/> 
          </
BBB
          <XXX> 
               <DDD> 
                    <EEE/> 
                    <DDD/> 
                    <CCC/> 
                    <FFF/> 
                    <FFF> 
                         <GGG/> 
                    </FFF> 
               </DDD> 
          </XXX> 
          <CCC> 
               <DDD/> 
          </CCC> 
     </AAA>

//CCC/preceding-sibling::*


     <AAA> 
          <
BBB
               <CCC/> 
               <DDD/> 
          </
BBB
          <
XXX
               <DDD> 
                    <
EEE/> 
                    <
DDD/> 
                    <CCC/> 
                    <FFF/> 
                    <FFF> 
                         <GGG/> 
                    </FFF> 
               </DDD> 
          </
XXX
          <CCC> 
               <DDD/> 
          </CCC> 
     </AAA>

 

 

 

 

实例 17

following轴(axis)包含同一文档中按文档顺序位于上下文节点之后的所有节点, 除了祖先节点,属性节点和命名空间节点

/AAA/XXX/following::*


     <AAA> 
          <BBB> 
               <CCC/> 
               <ZZZ> 
                    <DDD/> 
                    <DDD> 
                         <EEE/> 
                    </DDD> 
               </ZZZ> 
               <FFF> 
                    <GGG/> 
               </FFF> 
          </BBB> 
          <XXX> 
               <DDD> 
                    <EEE/> 
                    <DDD/> 
                    <CCC/> 
                    <FFF/> 
                    <FFF> 
                         <GGG/> 
                    </FFF> 
               </DDD> 
          </XXX> 
          <
CCC
               <
DDD/> 
          </
CCC
     </AAA>

//ZZZ/following::*


     <AAA> 
          <BBB> 
               <CCC/> 
               <ZZZ> 
                    <DDD/> 
                    <DDD> 
                         <EEE/> 
                    </DDD> 
               </ZZZ> 
               <
FFF
                    <
GGG/> 
               </
FFF
          </BBB> 
          <
XXX
               <
DDD
                    <
EEE/> 
                    <
DDD/> 
                    <
CCC/> 
                    <
FFF/> 
                    <
FFF
                         <
GGG/> 
                    </
FFF
               </
DDD
          </
XXX
          <
CCC
               <
DDD/> 
          </
CCC
     </AAA>

 

 

 

 

实例 18

preceding轴(axis)包含同一文档中按文档顺序位于上下文节点之前的所有节点, 除了祖先节点,属性节点和命名空间节点

/AAA/XXX/preceding::*


     <AAA> 
          <
BBB
               <
CCC/> 
               <
ZZZ
                    <
DDD/> 
               </
ZZZ
          </
BBB
          <XXX> 
               <DDD> 
                    <EEE/> 
                    <DDD/> 
                    <CCC/> 
                    <FFF/> 
                    <FFF> 
                         <GGG/> 
                    </FFF> 
               </DDD> 
          </XXX> 
          <CCC> 
               <DDD/> 
          </CCC> 
     </AAA>

//GGG/preceding::*


     <AAA> 
          <
BBB
               <
CCC/> 
               <
ZZZ
                    <
DDD/> 
               </
ZZZ
          </
BBB
          <XXX> 
               <DDD> 
                    <
EEE/> 
                    <
DDD/> 
                    <
CCC/> 
                    <
FFF/> 
                    <FFF> 
                         <GGG/> 
                    </FFF> 
               </DDD> 
          </XXX> 
          <CCC> 
               <DDD/> 
          </CCC> 
     </AAA>

 

 

 

 

实例 19

descendant-or-self 轴(axis)包含上下文节点本身和该节点的后代节点

/AAA/XXX/descendant-or-self::*


     <AAA> 
          <BBB> 
               <CCC/> 
               <ZZZ> 
                    <DDD/> 
               </ZZZ> 
          </BBB> 
          <
XXX
               <
DDD
                    <
EEE/> 
                    <
DDD/> 
                    <
CCC/> 
                    <
FFF/> 
                    <
FFF
                         <
GGG/> 
                    </
FFF
               </
DDD
          </
XXX
          <CCC> 
               <DDD/> 
          </CCC> 
     </AAA>

//CCC/descendant-or-self::*


     <AAA> 
          <BBB> 
               <
CCC/> 
               <ZZZ> 
                    <DDD/> 
               </ZZZ> 
          </BBB> 
          <XXX> 
               <DDD> 
                    <EEE/> 
                    <DDD/> 
                    <
CCC/> 
                    <FFF/> 
                    <FFF> 
                         <GGG/> 
                    </FFF> 
               </DDD> 
          </XXX> 
          <
CCC
               <
DDD/> 
          </
CCC
     </AAA>

 

 

 

 

实例 20

ancestor-or-self 轴(axis)包含上下文节点本身和该节点的祖先节点

/AAA/XXX/DDD/EEE/ancestor-or-self::*


     <
AAA
          <BBB> 
               <CCC/> 
               <ZZZ> 
                    <DDD/> 
               </ZZZ> 
          </BBB> 
          <
XXX
               <
DDD
                    <
EEE/> 
                    <DDD/> 
                    <CCC/> 
                    <FFF/> 
                    <FFF> 
                         <GGG/> 
                    </FFF> 
               </
DDD
          </
XXX
          <CCC> 
               <DDD/> 
          </CCC> 
     </
AAA>

//GGG/ancestor-or-self::*


     <
AAA
          <BBB> 
               <CCC/> 
               <ZZZ> 
                    <DDD/> 
               </ZZZ> 
          </BBB> 
          <
XXX
               <
DDD
                    <EEE/> 
                    <DDD/> 
                    <CCC/> 
                    <FFF/> 
                    <
FFF
                         <
GGG/> 
                    </
FFF
               </
DDD
          </
XXX
          <CCC> 
               <DDD/> 
          </CCC> 
     </
AAA>

 

 

 

 

实例 21

ancestor, descendant, following, preceding 和self轴(axis)分割了XML文档(忽略属性节点和命名空间节点), 不能交迭, 而一起使用则包含所有节点

//GGG/ancestor::*


     <
AAA
          <BBB> 
               <CCC/> 
               <ZZZ/> 
          </BBB> 
          <
XXX
               <
DDD
                    <EEE/> 
                    <
FFF
                         <HHH/> 
                         <GGG> 
                              <JJJ> 
                                   <QQQ/> 
                              </JJJ> 
                              <JJJ/> 
                         </GGG> 
                         <HHH/> 
                    </
FFF
               </
DDD
          </
XXX
          <CCC> 
               <DDD/> 
          </CCC> 
     </
AAA>

//GGG/descendant::*


     <AAA> 
          <BBB> 
               <CCC/> 
               <ZZZ/> 
          </BBB> 
          <XXX> 
               <DDD> 
                    <EEE/> 
                    <FFF> 
                         <HHH/> 
                         <GGG> 
                              <
JJJ
                                   <
QQQ/> 
                              </
JJJ
                              <
JJJ/> 
                         </GGG> 
                         <HHH/> 
                    </FFF> 
               </DDD> 
          </XXX> 
          <CCC> 
               <DDD/> 
          </CCC> 
     </AAA>

//GGG/following::*


     <AAA> 
          <BBB> 
               <CCC/> 
               <ZZZ/> 
          </BBB> 
          <XXX> 
               <DDD> 
                    <EEE/> 
                    <FFF> 
                         <HHH/> 
                         <GGG> 
                              <JJJ> 
                                   <QQQ/> 
                              </JJJ> 
                              <JJJ/> 
                         </GGG> 
                         <
HHH/> 
                    </FFF> 
               </DDD> 
          </XXX> 
          <
CCC
               <
DDD/> 
          </
CCC
     </AAA>

//GGG/preceding::*


     <AAA> 
          <
BBB
               <
CCC/> 
               <
ZZZ/> 
          </
BBB
          <XXX> 
               <DDD> 
                    <
EEE/> 
                    <FFF> 
                         <
HHH/> 
                         <GGG> 
                              <JJJ> 
                                   <QQQ/> 
                              </JJJ> 
                              <JJJ/> 
                         </GGG> 
                         <HHH/> 
                    </FFF> 
               </DDD> 
          </XXX> 
          <CCC> 
               <DDD/> 
          </CCC> 
     </AAA>

//GGG/self::*


     <AAA> 
          <BBB> 
               <CCC/> 
               <ZZZ/> 
          </BBB> 
          <XXX> 
               <DDD> 
                    <EEE/> 
                    <FFF> 
                         <HHH/> 
                         <
GGG
                              <JJJ> 
                                   <QQQ/> 
                              </JJJ> 
                              <JJJ/> 
                         </
GGG
                         <HHH/> 
                    </FFF> 
               </DDD> 
          </XXX> 
          <CCC> 
               <DDD/> 
          </CCC> 
     </AAA>

//GGG/ancestor::* | //GGG/descendant::* | //GGG/following::* | //GGG/preceding::* | //GGG/self::*


     <
AAA
          <
BBB
               <
CCC/> 
               <
ZZZ/> 
          </
BBB
          <
XXX
               <
DDD
                    <
EEE/> 
                    <
FFF
                         <
HHH/> 
                         <
GGG
                              <
JJJ
                                   <
QQQ/> 
                              </
JJJ
                              <
JJJ/> 
                         </
GGG
                         <
HHH/> 
                    </
FFF
               </
DDD
          </
XXX
          <
CCC
               <
DDD/> 
          </
CCC
     </
AAA>

 

 

 

 

实例 22

div运算符做浮点除法运算, mod运算符做求余运算, floor函数返回不大于参数的最大整数(趋近于正无穷), ceiling返回不小于参数的最小整数(趋近于负无穷)

//BBB[position() mod 2 = 0 ]

选择偶数位置的BBB元素


     <AAA> 
          <BBB/> 
          <
BBB/> 
          <BBB/> 
          <
BBB/> 
          <BBB/> 
          <
BBB/> 
          <BBB/> 
          <
BBB/> 
          <CCC/> 
          <CCC/> 
          <CCC/> 
     </AAA>

//BBB[ position() = floor(last() div 2 + 0.5) or position() = ceiling(last() div 2 + 0.5) ]

选择中间的BBB元素


     <AAA> 
          <BBB/> 
          <BBB/> 
          <BBB/> 
          <
BBB/> 
          <
BBB/> 
          <BBB/> 
          <BBB/> 
          <BBB/> 
          <CCC/> 
          <CCC/> 
          <CCC/> 
     </AAA>

//CCC[ position() = floor(last() div 2 + 0.5) or position() = ceiling(last() div 2 + 0.5) ]

选择中间的CCC元素


     <AAA> 
          <BBB/> 
          <BBB/> 
          <BBB/> 
          <BBB/> 
          <BBB/> 
          <BBB/> 
          <BBB/> 
          <BBB/> 
          <CCC/> 
          <
CCC/> 
          <CCC/> 
     </AAA>

C# xpath的更多相关文章

  1. xpath提取多个标签下的text

    title: xpath提取多个标签下的text author: 青南 date: 2015-01-17 16:01:07 categories: [Python] tags: [xpath,Pyth ...

  2. C#+HtmlAgilityPack+XPath带你采集数据(以采集天气数据为例子)

    第一次接触HtmlAgilityPack是在5年前,一些意外,让我从技术部门临时调到销售部门,负责建立一些流程和寻找潜在客户,最后在阿里巴巴找到了很多客户信息,非常全面,刚开始是手动复制到Excel, ...

  3. 在Java中使用xpath对xml解析

    xpath是一门在xml文档中查找信息的语言.xpath用于在XML文档中通过元素和属性进行导航.它的返回值可能是节点,节点集合,文本,以及节点和文本的混合等.在学习本文档之前应该对XML的节点,元素 ...

  4. XPath 学习二: 语法

    XPath 使用路径表达式来选取 XML 文档中的节点或节点集.节点是通过沿着路径 (path) 或者步 (steps) 来选取的. 下面列出了最有用的路径表达式: 表达式 描述 nodename 选 ...

  5. xpath 学习一: 节点

    xpath 中,有七种类型的节点: 元素.属性.文本.命名空间.处理指令.注释.以及根节点 树的根成为文档节点或者根节点. 节点关系: Parent, Children, sibling(同胞), A ...

  6. Python爬虫利器三之Xpath语法与lxml库的用法

    前面我们介绍了 BeautifulSoup 的用法,这个已经是非常强大的库了,不过还有一些比较流行的解析库,例如 lxml,使用的是 Xpath 语法,同样是效率比较高的解析方法.如果大家对 Beau ...

  7. 使用python+xpath 获取https://pypi.python.org/pypi/lxml/2.3/的下载链接

    使用python+xpath 获取https://pypi.python.org/pypi/lxml/2.3/的下载链接: 使用requests获取html后,分析html中的标签发现所需要的链接在& ...

  8. 关于robotframework,app,appium的xpath定位问题及常用方法

    关于类似的帖子好像很多,但是没有找到具体能帮我解决问题的办法.还是自己深究了好久才基本知道app上面的xpath定位和web上的不同点: 先放一个图: A,先说说不用xpath的场景,一般是用于存在i ...

  9. Selenium Xpath Tutorials - Identifying xpath for element with examples to use in selenium

    Xpath in selenium is close to must required. XPath is element locator and you need to provide xpath ...

  10. xpath定位中starts-with、contains和text()的用法

    starts-with 顾名思义,匹配一个属性开始位置的关键字 contains 匹配一个属性值中包含的字符串 text() 匹配的是显示文本信息,此处也可以用来做定位用 eg //input[sta ...

随机推荐

  1. Redis常用命令入门2:散列类型

    散列命令 散列类型的键值其实也是一种字典解耦,其存储了字段和字段值的映射,但字段值只能是字符串,不支持其他数据类型,所以说散列类型不能嵌套其他的数据类型.一个散列类型的键可以包含最多2的32次方-1个 ...

  2. cocoapod集成失败,无法找到头文件的解决办法

    在终端更新pod的时候,提示警告: target overrides the `OTHER_LDFLAGS` build setting defined in `Pods/Target Support ...

  3. php实现递归的三种方式: 遍历文件夹实例

    递归函数是我们常用到的一类函数,最基本的特点是函数自身调用自身,但必须在调用自身前有条件判断,否则无限无限调用下去.实现递归函数可以采取什么方式呢?本文列出了三种基本方式.理解其原来需要一定的基础知识 ...

  4. WebApi系列~基于RESTful标准的Web Api

    微软的web api是在vs2012上的mvc4项目绑定发行的,它提出的web api是完全基于RESTful标准的,完全不同于之前的(同是SOAP协议的)wcf和webService,它是简单,代码 ...

  5. 简单研究Loader笔记

    2015-11-11 18:25:34 1. Loader是什么? /** * Static library support version of the framework's {@link and ...

  6. 开启关闭keditor 过滤

    filterMode: false, K.create('#txt_content', { uploadJson: '/js/kindeditor-4.1.10/upload_json.ashx', ...

  7. HTML5 History API 实现无刷新跳转

     在HTML5中, 1. 新增了通过JS在浏览器历史记录中添加项目的功能. 2. 在不刷新页面的前提下显示改变浏览器地址栏中的URL. 3. 添加了当用户单击浏览器的后退按钮时触发的事件. 通过以上三 ...

  8. 【转】Memcached安装

    解析:Memcached是什么? Memcached是由Danga Interactive开发的,高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度. 一.软件版本    ...

  9. Scala 编程---类和对象

    类是对象的蓝图.一旦你定义了类,你就可以用关键字new从类的蓝图里创建对象.比方说,如果给出了类的定义: class ChecksumAccumulator { // class definition ...

  10. 通过js对cookie的使用手册

    一般大多数人还是用引用JQuary API——jquery.cookie.js,来操作cookie.这是一种很不错的方式,我也支持这样的做法. 但是有时候我们只需要一种极为轻量级的代码来实现简单的功能 ...