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. SQL Server 2012提供的OFFSET/FETCH NEXT与Row_Number()对比测试 [T]

    SQL Server 2008中SQL应用系列--目录索引 前些天看到一篇文章<SQL Server 2012 - Server side paging demo using OFFSET/FE ...

  2. 第一篇博客:Hello World

    2016年10月10日,双十,好日子,决定开始写第一篇博客,标题想了会,就叫Hello World 吧,哈哈^_^. 首先感谢博客园的管理们能批准我的申请,记得在14年的时候申请过一次,竟然没申请通过 ...

  3. expdp/impdp

    1.c:\>sqlplus user1/password@db2.sql>create directory dmpdp as 'C:\data';本地创建文件夹,然后dmp文件放到这个文件 ...

  4. Socket Receive 避免 Blocking

    我们知道 Socket Blocking 属性默认true . 表明Socket 处于同步调用 , Connect , 或 Send , Receive 需等待动作 完成才能继续执行. 有一种应用场景 ...

  5. 字符设备驱动之Led驱动学习记录

    一.概述 Linux内核就是由各种驱动组成的,内核源码中大约有85%的各种渠道程序的代码.一般来说,编写Linux设备驱动大致流程如下: 1.查看原理图,数据手册,了解设备的操作方法. 2.在内核中找 ...

  6. JavaScript 闭包系列二(匿名函数及函数的闭包)

    一. 匿名函数 1. 函数的定义,可分为三种 1) 函数声明方式 function double(x) {     return 2*x; } 2)Function构造函数,把参数列表和函数体都作为字 ...

  7. Dev 13.2 汉化教程(提供汉化cs文件下载)

    主要为了汉化 FindPanel里的Find和Clear控件名称,研究了一些时间. 废话不说.直接上干货. 1.已将cs文件分享,下载放到项目里. 下载链接: http://pan.baidu.com ...

  8. 关于LockSupport

    concurrent包的基础 Doug Lea 的神作concurrent包是基于AQS (AbstractQueuedSynchronizer)框架,AQS框架借助于两个类:Unsafe(提供CAS ...

  9. 在CentOS6.8上面安装Python3.5

    以前每次装Linux,升级Python,都会一堆问题,然后Google,本来想着记录一下,结果问题太多了,也就记不住了,这次特地记了下来. 在CentOS6.8上面安装Python3.5我的系统是Ce ...

  10. CPU的大小端模式

    不同体系结构的CPU,数据在内存中存放的排列顺序是不一样的. 存储器中对数据的存储是以字节(Byte)为基本单位的,因此,字(Word)和半字(Half-Word)在存储器中就有两种次序,分别称为:大 ...