果断收藏~xpath详解

转载 2013年12月05日 12:04:14

我的漫漫程序之旅

专注于JavaWeb开发

XPath详解

以下是本人找到的最完整最易懂的XPath教程,不敢私藏,拿出来与大家分享。

实例 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>



posted on 2008-04-25 17:02 々上善若水々 阅读(909) 评论(0)  编辑  收藏 所属分类: WebService

公告

导航

< 2013年12月 >
24 25 26 27 28 29 30
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 1 2 3 4

统计

  • 随笔 - 37
  • 文章 - 309
  • 评论 - 357
  • 引用 - 0

留言簿(34)

随笔档案(42)

文章分类(304)

文章档案(256)

搜索

  •  

最新评论 

阅读排行榜

评论排行榜

XPath实例详解

XPath 是XML的查询语言,和SQL的角色很类似。以下面XML为例,介绍XPath 的语法。       Empire Burlesque     Bob Dylan     10.90...
  • sven_xu
  • sven_xu
  • 2015年06月02日 09:20
  • 1713

XPath详解,总结

经常在工作中会使用到XPath的相关知识,但每次总会在一些关键的地方不记得或不太清楚,所以免不了每次总要查一些零碎的知识,感觉即很烦又浪费时间,所以对XPath归纳及总结一下。 在这篇文章中你将...
  • z69183787
  • z69183787
  • 2016年11月17日 15:09
  • 2071

libxml2用xpath进行查找

xml文档             Bayern         http://mp3.webradio.antenne.de:80                     usa   ...
  • zmlovelx
  • zmlovelx
  • 2012年10月24日 14:46
  • 4845

xpath的extract()方法

extract()经常使用来切片(脱壳)从一个对象中得到list 下面是scrapy的官方文档,里面有选择器(selector)的一些介绍 http://scrapy-chs.readthedoc...
  • HAZER644
  • HAZER644
  • 2017年12月16日 17:10
  • 171

Python中 etree.xpath实践

先记录下python的一个陷阱 如图: python获取字典里不存在的字段返回None None是一个特殊的常量。 None和False不同。 None不是0。 None不是空字符...
  • zlp1992
  • zlp1992
  • 2015年04月07日 15:28
  • 4877

xpath定位方法详解

1.xpath较复杂的定位方法: 现在要引用id为“J_password”的input元素,可以像下面这样写:WebElement password = driver.findElement(B...
  • hqq1990
  • hqq1990
  • 2016年11月10日 11:07
  • 730

Scrapy 入门学习笔记(2) -- xpath 与 css 解析以及解析网页示例

最近学习用 Scrapy 框架写爬虫,简单来说爬虫就是从网上抓取网页,解析网页,然后进行数据的存储与分析,将从网页的解析到数据的转换存储。将学习过程中用到的解析技术,Scrapy 的各个模块使用与进阶...
  • Ahri_J
  • Ahri_J
  • 2017年05月15日 20:33
  • 4867

Java高级特性之XML

XML 简介、编写规范、Java 解析 XML(DOM 解析 XML、DOM4J 解析 XML)……关于 Java 高级特性之 XML 比较详细、系统的整理。...
  • FatliTalk
  • FatliTalk
  • 2016年12月20日 14:36
  • 470

Favor库简述

Favor是一个第三方库, 可以简化SharedPreferences, 使用依赖注入的方式声明. 我们根据常见的输入框, 把账户信息存在Favor的类中, 来看看这个库怎么用. 1. 准备配置bui...
  • u012515223
  • u012515223
  • 2015年11月08日 20:53
  • 1825

xpath解析使用extract()的各种情况分析

返回一个SelectorList 对象 http://scrapy-chs.readthedocs.io/zh_CN/0.24/topics/selectors.html#selectorlist S...
  • nzjdsds
  • nzjdsds
  • 2017年08月16日 21:17
  • 983
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:果断收藏~xpath详解
举报原因:
原因补充:

(最多只允许输入30个字)